Cron and Systemd Timers
Automation should be deterministic: same user, same config path, same logs, every run.
Learning Focus
Build schedulers that are boring: same environment, predictable config, and clean failure signals.
Production Requirements
| Requirement | Why |
|---|---|
| Absolute paths | Cron has minimal environment |
| Explicit config file | Avoid user-context surprises |
| Log to file (and/or journald) | Incident debugging |
| Locking | Prevent overlapping destructive jobs |
| Exit-code awareness | Alert on failure |
Mapping
Cron Example
crontab
0 2 * * * /usr/bin/rclone sync /srv/data remote-prod:backup/data --config /etc/rclone/rclone.conf --log-file /var/log/rclone-nightly.log
warning
Cron does not automatically capture stderr/stdout unless you redirect or configure mail. Always log.
Systemd Unit Example
/etc/systemd/system/rclone-backup.service
[Unit]
Description=Nightly Rclone Backup
[Service]
Type=oneshot
ExecStart=/usr/bin/rclone sync /srv/data remote-prod:backup/data --config /etc/rclone/rclone.conf --log-file /var/log/rclone-nightly.log
Add Preflight + Locking (Recommended)
/etc/systemd/system/rclone-backup.service
[Unit]
Description=Nightly Rclone Backup
[Service]
Type=oneshot
User=rezriz
Group=rezriz
ExecStart=/usr/bin/flock -n /var/lock/rclone-backup.lock \
/usr/bin/rclone sync /srv/data remote-prod:backup/data \
--config /etc/rclone/rclone.conf \
--log-file /var/log/rclone-nightly.log
/etc/systemd/system/rclone-backup.timer
[Unit]
Description=Run nightly rclone backup
[Timer]
OnCalendar=*-*-* 02:00:00
Persistent=true
[Install]
WantedBy=timers.target
Observability
Useful commands:
systemctl status rclone-backup.service
journalctl -u rclone-backup.service --since "today"
systemctl list-timers --all | grep rclone
Cron vs Systemd
| Topic | Cron | Systemd timer |
|---|---|---|
| Logging | Manual redirection | Journald + file logs |
| Missed-run behavior | None by default | Persistent=true catch-up |
| Dependency controls | Limited | Strong unit ordering and checks |
tip
Prefer systemd timers on modern Linux hosts for better observability and restart controls.
Common Pitfalls
| Pitfall | Failure mode | Fix |
|---|---|---|
| Job runs as root unexpectedly | Config differs from manual test | Use User= and explicit --config |
| Relative paths | Works manually, fails in cron | Use absolute paths only |
| No locking | Overlapping syncs | Wrap in flock |
| No alerting | Silent failure | Monitor logs and exit code |