Cron and scheduled jobs
You add a line to a crontab and a script runs every night at three. Until the night it does not run at all, or runs on three machines at once. Here is what cron really checks, drawn one minute at a time.
01 Five fields and a command
Each crontab line is five time fields and then a command. A field is * (any value), a number, a list 1,15, a range 1-5 or a step */15 (every 15th value, starting from the lowest).
| Schedule | Runs |
|---|---|
| */15 * * * * | At minutes 0, 15, 30 and 45 of every hour |
| 0 3 * * * | Every day at 03:00 |
| 30 9 * * 1-5 | At 09:30, Monday to Friday (0 and 7 both mean Sunday) |
| 0 0 1 * * | At midnight on the 1st of each month |
| 0 0 13 * 5 | Midnight on every 13th **and** every Friday, not only Friday the 13th |
The last row surprises people. In the classic cron (Vixie cron and its descendants such as cronie), when both day fields are restricted the job runs if either matches. Times are in the machine’s time zone unless your cron supports a CRON_TZ setting.
02 What the daemon actually does
The cron daemon is a simple loop. Typically it sleeps until the start of the next minute, wakes, compares every entry with the current time, starts the matching commands, and sleeps again. It has no queue of upcoming runs and no memory of past ones.
- this minute
- checked, no match
- matches, runs
- not reached yet
The daemon has read the crontab and is asleep until the next minute starts. Nothing has run.
Two things follow from this loop. A run happens only if the daemon is awake in that minute. And cron starts a command without asking whether the previous run of it has finished.
03 Missed runs are silently skipped
Now the machine reboots across 03:00. Watch what cron does when it comes back.
- this minute
- checked
- machine down
- not reached yet
The backup entry is 0 3 * * *. Only one minute of the day matches it: 03:00.
anacron does the same for daily, weekly and monthly jobs.Kubernetes CronJobs have a related knob, startingDeadlineSeconds: a run that could not start in time is either started late, within that window, or counted as missed. Whatever runs your schedule, ask: what happens to the run I missed?
Daylight saving time has the same effect. When clocks jump from 02:00 to 03:00, a job at 30 2 * * * has no matching minute; when they fall back, 01:30 happens twice. Implementations handle this differently, so run servers and schedules in UTC.
04 Running on exactly one machine
Put the crontab in your server image and every server runs the job. For cleanup that is merely wasteful; for invoices it is a bug. The fix is a guard that lets exactly one machine claim each run. A row with a unique key per run does this well.
- cron fired
- won the run
- lost, exits
- just written
- conflict
Every web server got the same crontab at deploy. The send_invoices entry is due at 03:00. The job_runs table is empty.
| Approach | How | Watch out for |
|---|---|---|
| One scheduler machine | Only one box has the crontab | When it is down, nothing runs |
| Unique run key | Insert `(job, run_for)` first; only the winner runs | If the winner crashes, the row says `running` forever; mark stale ones and retry |
| Lock while running | `pg_try_advisory_lock(42)` or Redis `SET key id NX PX 600000` | Stops overlap, not repeats: a machine whose clock is a few seconds late can take the lock after the first one releases it |
| A managed scheduler | Kubernetes CronJob, a cloud scheduler | Kubernetes documents that a run may, rarely, start twice or not at all, so jobs should still be idempotent |
05 A run that outlasts its interval
A sync job runs every 5 minutes and usually takes 2. One day the API it calls is slow and a run takes 7 minutes. Cron starts the next run at the 5-minute mark anyway, and now two copies work on the same data. On Linux, flock from util-linux stops this: -n makes the new copy give up at once if the lock file is held.
*/5 * * * * /usr/local/bin/sync.sh
*/5 * * * * flock -n /tmp/sync.lock /usr/local/bin/sync.sh
flock only sees one machine’s files. Across machines you need the shared guards from the previous section.
06 See it on your own machine
crontab -l lists your entries and crontab -e edits them. On Debian and Ubuntu, cron logs one line per command it starts. That line proves cron fired; it says nothing about whether the command worked.
Note the >> file 2>&1 on each line. Without it, cron tries to email the output to the user, and on most servers no mail is set up, so the output (and every error) is lost.
07 When it bites
0 3 * * * node backup.jsWorks in your shell, fails in cronCron runs commands with a tiny environment: typically PATH=/usr/bin:/bin, no shell profile, home directory as the working directory. Use full paths, or set PATH at the top of the crontab.0 3 * * * tar czf /b/$(date +%F).tgz /dataA percent sign in the commandIn a crontab line, % means “newline”, so this command is cut short. Escape it as \%, or move the command into a script.job_runs row, a heartbeat check) and alert when the report is missing.