Notes
crontab
cron reads a table of jobs, the crontab. Each line has five time fields, minute, hour, day of month, month and day of week, then a command. Thirty two star star star runs at two thirty every night. Star slash fifteen in the minute field means every fifteen minutes. Zero three star star zero means three in the morning on Sundays, since day zero is Sunday. crontab minus e edits your table, and minus l lists it.
# m h dom mon dow command 30 2 * * * backup.sh */15 * * * * check.sh 0 3 * * 0 weekly.sh 0 9 1 * * report.sh minute hour day-of-month month day-of-week (0 = Sun) crontab -l list yours
Why cron jobs fail
Jobs that work in your terminal often fail in cron, because cron is not your shell. It runs with a tiny PATH, so commands outside user bin and bin are not found. Your profile and aliases are not loaded. Output is mailed to nobody or lost, so failures are silent. Nothing stops a slow run from overlapping the next one. And the time zone is the server's, usually UTC. Use full paths, log to a file, and add a lock.
- a tiny PATH: /usr/bin:/bin → "command not found"
- no shell profile, no aliases
- output mailed or lost
- no lock: a slow run overlaps the next one server time zone, often UTC
- fix: full paths, log to a file, flock, test the exact command with env -i
Make it reliable
Here is a safe cron line. flock minus n takes a lock file and skips the run if the previous one still holds it, so runs never overlap. The script is called by its full path. Both output and errors are appended to a log file, using the redirection from episode five. And inside the script, strict mode, which part three explains, makes any failed command stop the job with an error code.
30 2 * * * /usr/bin/flock -n /run/lock/backup.lock /opt/shoprung/bin/backup.sh >> /var/log/shoprung/backup.log 2>&1 flock -n: skip if still running append both streams to a log script: set -euo pipefail
systemd timers
systemd timers are the modern alternative. A timer unit says when, and starts a service unit with the same name that says what. Ours runs at two thirty every night, with a random delay of up to ten minutes so a fleet of servers does not hit the database at once. Persistent true means if the machine was off at two thirty, the job runs as soon as it boots. systemd analyze verify checked both files and found no problems.
[Timer]
OnCalendar=*-*-* 02:30:00
RandomizedDelaySec=10m
Persistent=true
[Install]
WantedBy=timers.target
$ systemd-analyze verify
backup.timer backup.service
(no problems found) Check the schedule
Never guess what a schedule means; ask. systemd analyze calendar prints the next time an expression will fire, in local time and UTC. On our machine, two thirty every day was next due on Thursday at two thirty India time, which is nine in the evening UTC the day before, a classic time zone surprise. Sunday at three was next on the sixth. And the shortcut weekly means Monday at midnight, which surprised some of us.
"*-*-* 02:30:00"
Next: Thu 2026-12-03 02:30 IST
(Wed 21:00 UTC)
"Sun *-*-* 03:00"
Next: Sun 2026-12-06 03:00 IST
"weekly"
= Mon *-*-* 00:00:00
"hourly" = *-*-* *:00:00 Why timers win
Why do timers win? systemctl list timers shows every timer with its next and last run. status shows whether the last run succeeded, with its exit code. Every run's output goes to the journal. A service that is still running is not started again, so runs cannot overlap. And the job gets everything a service has: its own user, memory limits and sandboxing. cron is still everywhere, so you need both.
$ systemctl list-timers NEXT, LEFT, LAST, PASSED $ systemctl status backup last result, exit code $ journalctl -u backup every run's output no overlap: a running service is not started twice User=, limits, sandboxing
Know when it did not run
The most important habit: alert when a job did not succeed, not only when it fails loudly. A job that silently never runs looks exactly like one that worked. So at the end of a successful run, ping a monitor, a heartbeat. If no ping arrives by the deadline, the monitor alerts. This is called a dead man's switch. And for backups, schedule test restores too, since a backup you have never restored is only a hope.
- a silent job looks like a successful one on success: ping a monitor (healthchecks, Prometheus pushgateway, a heartbeat)
- no ping by the deadline → alert
- test restores, not just backups
Common mistakes
Mistakes. Relying on PATH in cron. No log and no check of the exit code. Long jobs that overlap. Forgetting the server's time zone. Every server running its job at exactly midnight. No alert when a job quietly stops. And backups that are never test-restored.
- relying on PATH in cron
- no log, no exit code check
- overlapping runs (no flock)
- forgetting the time zone
- every server at 00:00 exactly
- no alert when a job stops
- backups never restored
Say it like this
In the interview: both work. I prefer systemd timers for new jobs, because they log to the journal, show the last result, never overlap, can catch up missed runs with persistent, add a random delay, and get service sandboxing. In cron I use full paths, flock and a log file. Either way, the job pings a heartbeat on success, so we get an alert if it did not run.
- "Both work. Timers give logs in the journal, last status, no overlap, Persistent for missed
- runs, random delay, and service
- sandboxing. In cron I use full
- paths, flock and a log file.
- Either way, a heartbeat alerts
- if the job did not succeed."