Cron and Systemd Timers¶
Cron is the traditional approach, systemd timers are the modern alternative with better integration.
Cron¶
crontab -e
m h dom mon dow command¶
0 2 * * * /opt/backup.sh */5 * * * * /opt/check.sh 0 2 * * * /opt/backup.sh >> /var/log/backup.log 2>&1 * * * * * flock -n /tmp/task.lock /opt/task.sh
Systemd Timers¶
backup.timer¶
[Timer] OnCalendar=*-*-* 02:00:00 Persistent=true [Install] WantedBy=timers.target
backup.service¶
[Service] Type=oneshot ExecStart=/opt/backup.sh systemctl enable –now backup.timer systemctl list-timers
Comparison¶
- Logs: cron -> syslog, timer -> journalctl
- Dependencies: timer -> After=, Requires=
- Resource limits: timer -> via service
- Persistent: timer catches up on missed runs
Timers for New Projects¶
Use systemd timers for new projects. Cron for simple legacy tasks.