Schedule tasks with cron and systemd timers
Updated 27 Nov 2026 cron automation
Run scripts on a schedule and know when they fail.
Cron
crontab -e
Five fields are minute, hour, day of month, month and weekday:
30 2 * * * /home/deploy/backup.sh >> /home/deploy/backup.log 2>&1
*/5 * * * * /usr/bin/flock -n /tmp/worker.lock /home/deploy/worker.sh
flock -n stops a second copy starting while the first still runs.
Cron has a small environment: use full paths and set variables in the script. Times follow the server's time zone.
systemd timer
/etc/systemd/system/backup.service:
[Service]
Type=oneshot
User=deploy
ExecStart=/home/deploy/backup.sh
/etc/systemd/system/backup.timer:
[Timer]
OnCalendar=*-*-* 02:30:00
Persistent=true
[Install]
WantedBy=timers.target
sudo systemctl daemon-reload
sudo systemctl enable --now backup.timer
systemctl list-timers
journalctl -u backup.service
Persistent=true runs a missed job after the server was off. Timers also record output in the journal, which makes failures easier to see.