Moving my cron jobs to systemd timers
I resisted this for years because crontab is one line and a timer is two files. What changed my mind was a backup job that had been failing silently for eleven days, with its output mailed to a local mailbox nobody read.
A timer and its service
# /etc/systemd/system/backup.service
[Unit]
Description=Nightly restic backup
[Service]
Type=oneshot
ExecStart=/usr/local/bin/backup.sh
# /etc/systemd/system/backup.timer
[Unit]
Description=Run backup nightly
[Timer]
OnCalendar=*-*-* 03:15
RandomizedDelaySec=20m
Persistent=true
[Install]
WantedBy=timers.target
Enable with systemctl enable --now backup.timer.
What I gained
- Logs in one place.
journalctl -u backupshows every run with its exit status. Persistent=trueruns a missed job at next boot, which matters for machines that are sometimes off.RandomizedDelaySecstops five servers from hitting the backup host at exactly 03:15.- Failure hooks.
OnFailure=can trigger a notification unit, which is how the silent-failure problem actually got fixed.
What I miss
Seeing every scheduled job on one screen. systemctl list-timers comes close, and I have stopped missing it much.