nya notes

Replacing cron jobs with systemd timers

For years every box I ran had a crontab full of one-liners. They work, but failures are silent and logs end up wherever. Timers fix both.

A timer is two units: a .service that does the work and a .timer that schedules it.

# /etc/systemd/system/backup.service
[Unit]
Description=Nightly backup

[Service]
Type=oneshot
ExecStart=/usr/local/bin/backup.sh
# /etc/systemd/system/backup.timer
[Timer]
OnCalendar=*-*-* 03:30
RandomizedDelaySec=15m
Persistent=true

[Install]
WantedBy=timers.target

Persistent=true runs a missed job after boot, and journalctl -u backup gives you the output. systemctl list-timers shows when everything fires next.

← All posts