Skip to content

Cron and scheduled jobs

Working knowledgeUnder the hood

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).

Text
# minute hour day-of-month month day-of-week command
*/15 * * * * /usr/local/bin/cleanup.sh
0 3 * * * /usr/local/bin/backup.sh
30 9 * * 1-5 /usr/local/bin/report.sh
Reading a schedule
ScheduleRuns
*/15 * * * *At minutes 0, 15, 30 and 45 of every hour
0 3 * * *Every day at 03:00
30 9 * * 1-5At 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 * 5Midnight 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.

The cron daemon checks every entry, once a minute
Step 1 of 5
clock (one cell per minute)
02:58
02:59
03:00
03:01
crontab entries
*/15 * * * *
0 3 * * *
30 9 * * 1-5
commands started
Empty
  • 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.

A reboot across 03:00 skips the backup
Step 1 of 7
clock on db-1
02:58
02:59
03:00
03:01
03:02
03:03
backup runs today
Empty
  • this minute
  • checked
  • machine down
  • not reached yet

The backup entry is 0 3 * * *. Only one minute of the day matches it: 03:00.

0 3 * * * entry
Plain cronSkippedA minute the daemon did not see is gone. The job waits for the next match.
systemd timer, Persistent=trueRuns at bootsystemd stores the last run time on disk and starts a missed run as soon as the timer is active again. 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.

claim_run.sql
CREATE TABLE job_runs (
job text NOT NULL,
run_for date NOT NULL,
status text NOT NULL DEFAULT 'running',
PRIMARY KEY (job, run_for)
);
-- Each machine runs this first. A row back means "you won".
INSERT INTO job_runs (job, run_for)
VALUES ('invoices', '2026-11-02')
ON CONFLICT DO NOTHING
RETURNING job;
Three machines race for one run key
Step 1 of 7
servers, same crontab
web-1
web-2
web-3
job_runs table
Empty
invoice batches sent
Empty
  • 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.

Ways to get one run per schedule
ApproachHowWatch out for
One scheduler machineOnly one box has the crontabWhen it is down, nothing runs
Unique run keyInsert `(job, run_for)` first; only the winner runsIf 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 schedulerKubernetes CronJob, a cloud schedulerKubernetes 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.

Can overlap
*/5 * * * * /usr/local/bin/sync.sh
One at a time
*/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.

Shell
$ grep CRON /var/log/syslog | tail -2
Nov 2 03:00:01 web-1 CRON[2816]: (deploy) CMD (/usr/local/bin/cleanup.sh >> /var/log/cleanup.log 2>&1)
Nov 2 03:00:01 web-1 CRON[2817]: (deploy) CMD (/usr/local/bin/backup.sh >> /var/log/backup.log 2>&1)
# on systemd machines
$ journalctl -u cron --since today # the unit is crond on RHEL and Fedora

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.
A failure nobody seesA skipped or failing job raises no alarm. Have each run report success somewhere (a job_runs row, a heartbeat check) and alert when the report is missing.
One crontab on every serverScaling from one server to three silently triples every job. Claim each run with a unique key, or move schedules to one place.

08 Check yourself

Question

A server is down from 02:55 to 03:10. When does plain cron run a 0 3 * * * job?

Question

Three servers each take a Postgres advisory lock before the 03:00 job and release it when done. The job takes 2 seconds. Can it run twice?

Question

What does 0 0 1 * 1 mean in classic cron?

09 Practise this

Was this tutorial useful?Your vote tells us what to improve next.
Spotted a mistake, or something unclear?