Why Systemd Timers?
Systemd timers offer advantages over cron: better logging (journalctl), dependency management, resource controls, and the ability to trigger missed runs.
Create a Service Unit
Create /etc/systemd/system/backup.service:
[Unit]
Description=Daily Backup Job
[Service]
Type=oneshot
ExecStart=/root/scripts/backup.sh
User=rootCreate a Timer Unit
Create /etc/systemd/system/backup.timer:
[Unit]
Description=Run backup daily at 3 AM
[Timer] 03:00:00
Persistent=true
RandomizedDelaySec=300
[Install]
WantedBy=timers.targetEnable the Timer
sudo systemctl daemon-reload
sudo systemctl enable --now backup.timerOnCalendar Syntax
# Every 5 minutes
# Daily at midnight
# Monday at 9 AM *-*-* 09:00:00
# First of every month 00:00:00Managing Timers
# List all timers
systemctl list-timers --all
# View logs
journalctl -u backup.service
# Run manually
sudo systemctl start backup.service