DEVOPS, SRE AND CLOUD · PART 1 · LINUX, FROM THE INSIDE · SERVICES · 24 OF 187

cron vs systemd Timers: Scheduling Jobs on Linux

How to run jobs on a schedule: crontab syntax field by field, where cron runs jobs and why they fail (no PATH, no output, overlapping runs), systemd timers as the modern alternative with logs, Persistent=true and random delay, a nightly ShopRung backup timer that passed verify, schedules checked with systemd-analyze calendar (weekly means Monday 00:00), time zones, flock, and alerts when a job did not run.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
1 / 11

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • Five cron fields
  • full paths, flock, logs
  • timer + service
  • Persistent, random delay
  • calendar to check
  • heartbeat alerts

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.

crontab -e
# 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.

cron runs with
  • 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.

A safe cron line
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.

backup.timer + backup.service
[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.

systemd-analyze calendar (ours)
"*-*-* 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.

Timers give you
$ 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.

Dead man's switch
  • 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.

Avoid these
  • 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.

In the interview
  • "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."

Interview questions

Q1Explain the cron fields.

Minute, hour, day of month, month, day of week (0 or 7 = Sunday), then the command; */15 means every 15 units.

Q2Why does a script work in the terminal but not in cron?

cron runs with a minimal environment: short PATH, no profile, different working directory, no TTY. Use full paths, set variables explicitly and log output.

Q3How do you stop cron jobs from overlapping?

Wrap the command in flock -n with a lock file, so a run is skipped while the previous one is still running.

Q4What are systemd timers?

Timer units that start a matching service unit on a calendar (OnCalendar) or relative schedule, with logs in the journal and status in systemctl list-timers.

Q5What does Persistent=true do?

If the machine was off when the timer should have fired, the job runs as soon as possible after boot.

Q6How do you check when a schedule will fire?

systemd-analyze calendar "<expression>" shows the normalized form and next elapse in local time and UTC.

Q7How do you know a nightly job actually ran?

Have it ping a heartbeat/dead man's switch on success and alert if the ping is missing; check systemctl list-timers or logs.

Q8Why add RandomizedDelaySec?

To spread a fleet's jobs over a window instead of all hitting shared systems at the same second.