Why systemd timers beat cron
- No overlapping runs. If
cronlaunchesdeploy.shevery 10 minutes and the previous run stalls ondocker compose pull,cronspawns a second copy on top of the first — forcing you to wrap every job inflock. A systemd timer activates aType=oneshotservice; while it is still running, the next trigger is skipped automatically. - Centralized logs. In
cron, unredirected output is lost or piles up in localrootmail. A timer sends stdout and stderr straight tojournalctl -u <service>with timestamps, exit codes, and journal size limits. - Missed runs catch up. If the server was rebooting at 03:00,
cronskips the nightly backup until the next night. A timer withPersistent=truerecords its last run on disk and runs the missed job right after boot. - Dependencies, alerts, and sandboxing. The timer’s service waits for networking and Docker (
After=network-online.target docker.service), can run inside a sandbox (systemd sandbox), can alert on failure viaOnFailure=(OnFailure= in systemd services), and can be triggered by hand withsystemctl start <name>.servicein the exact environment used on schedule. - Tracked in git. Instead of editing
crontab -edirectly on the server, units live in/etc/systemd/system/with copies tracked underhost/etc/systemd/system/ininfra-web1(Server configs in git).
A .service and .timer pair
A timer and service with the same base name bind to each other automatically: deploy.timer triggers deploy.service (example from Deploy without SSH).
# /etc/systemd/system/deploy.service
[Unit]
Description=Deploy /opt stacks from git
Wants=network-online.target
After=network-online.target docker.service
[Service]
Type=oneshot
ExecStart=/opt/scripts/deploy.sh
Environment=HOME=/root
# /etc/systemd/system/deploy.timer
[Unit]
Description=Deploy from git every 10 minutes
[Timer]
OnCalendar=*:0/10
[Install]
WantedBy=timers.target
Two other options commonly added to [Timer]:
Persistent=true— for infrequent jobs like a nightly backup (OnCalendar=*-*-* 03:00), so a run missed during a reboot executes once the server comes back up. Frequent timers (every 10 minutes) do not need it.RandomizedDelaySec=5m— spreads triggers across a random window when multiple servers hit the same storage or API at the same hour.
Enable the timer, not the service (leave [Install] out of .service):
sudo systemctl daemon-reload
sudo systemctl enable --now deploy.timer
Check complex schedules with systemd-analyze calendar before enabling:
systemd-analyze calendar --iterations=3 'Sun *-*-* 05:30'
It prints the normalized expression and the next three trigger times in the server’s time zone.
When cron still stays
Minimal containers (Alpine, Debian slim) do not include systemd, so use cron or supercronic there. You also do not need to migrate packaged system jobs in /etc/cron.d/ by hand.
If it doesn’t work
Created a timer, but it never triggers on schedule and does not appear in systemctl list-timers
- Cause
- You forgot
systemctl daemon-reload, or enabled the.serviceinstead of the.timer. - Fix
- Run
sudo systemctl daemon-reload && sudo systemctl enable --now <name>.timerand check the row insystemctl list-timers <name>.timer.
A script fails when run from a timer, even though it works in an interactive root shell
- Cause
- A systemd service gets a minimal environment: a narrower
PATH, and withoutUser=noHOMEvariable — sogit,ssh, orkopiacannot find configs in/root. - Fix
- Add
Environment=HOME=/rootunder[Service]and verify full binary paths. Test withsudo systemctl start <name>.serviceand inspectjournalctl -u <name>.service -n 30.
An OnCalendar= timer fired at a different hour than expected
- Cause
- The server runs in
UTCwhile the schedule was written in local time, or theOnCalendar=syntax is off. - Fix
- Check the server’s time zone with
timedatectland validate the expression withsystemd-analyze calendar --iterations=3 '<expression>'.
A nightly backup was missed during a server reboot and did not catch up after boot
- Cause
[Timer]is missingPersistent=true, so systemd waits for the next scheduled window.- Fix
- Add
Persistent=trueunder[Timer], then runsudo systemctl daemon-reloadandsudo systemctl restart <name>.timer.
How to check
Confirm the timer is active and has a next run scheduled:
systemctl list-timers deploy.timer
NEXT and LEFT show the countdown to the next run, and ACTIVATES lists deploy.service.
Run the service manually in the exact timer environment and inspect the journal:
sudo systemctl start deploy.service
journalctl -u deploy.service -n 20 --no-pager
The output should end with Finished deploy.service, with no environment or permission errors.