How to delay systemd unit at boot/login?
I have a systemd unit that runs hourly, but when the computer has just booted/logged in it will "catch up" on the previous run, so it ends up firing right after I log in.
Is there a way to make it wait a minute before attempting to start? It should only when catching up just after a boot. Otherwise, when running on schedule or manually triggered, it shouldn't delay itself.
For context, I have a DNS issue with my unit. Of course, I could add the proper targets, but it seems easier to just delay it a bit. Also, delaying is useful in other contexts, like wanting to avoid performance intensive tasks right after boot.
1 answer
You can use ExecStartPre= to delay the execution of the systemd unit.
Use systemctl edit myunit.service
to create a drop in file and add the following lines:
[Service]
ExecStartPre=/bin/sleep 300
This will delay the execution of the actual Exec=
line by 5 minutes (300 seconds).
To prevent the execution of the delay on manual starts you can check the uptime:
[Service]
ExecStartPre=bash -c 'if [ $(awk \'{print int($1)}\' /proc/uptime) -lt 600 ]; then /bin/sleep 300; fi'
0 comment threads