Backend / Cron jobs

Cron jobs

How a server runs a task at a set time with nobody there. Seven steps read the five fields of a cron expression, then show what time zones, slow jobs and downtime do to it.

Beginner7 steps~6 min
Cron — checks the clock once a minute
Field — one of five parts of the schedule
Job — the command cron starts
Run — one execution of the job
// THE SHORT VERSION

In 30 seconds

A cron job is a command that a server starts by itself on a schedule, such as a backup every night.

The schedule is five fields: minute, hour, day of month, month and day of week. A star means every value, so 30 2 * * * means 02:30 every day.

Cron only looks at the clock. It uses the server’s time zone, starts a new run even if the last one is still going, and does not make up for runs missed while the machine was off.

Cron jobs, step by step

STEPS01 / 07

This is classic Unix cron. Some schedulers add a seconds field or shortcuts like @daily. If both day fields are set, cron runs when either one matches. Daylight-saving handling varies between versions.

STEP 01 / 07 A task that starts itself
THE MATH
every minute: time matches a line? yes → start its command
  1. Step 1: A task that starts itself. Some work has to happen on a schedule with nobody pressing a button, such as a nightly backup. On Unix-like servers a small program called cron does this: it wakes once a minute, compares the clock with a list of schedules, and starts any command whose time has come.
  2. Step 2: Five fields say when. A schedule is written as five fields separated by spaces: minute, hour, day of the month, month, and day of the week. Each field holds a number in its range, or a star meaning every value. After the five fields comes the command to run.
  3. Step 3: Reading 30 2 * * *. Read it field by field. Minute 30, hour 2, and stars for the rest: any day of the month, any month, any day of the week. Every field must match at once, so the command runs at 02:30 each day and at no other minute.
  4. Step 4: Common schedules. A few symbols cover most needs. A star with a slash sets a step, so */5 in the minute field means minutes 0, 5, 10 and so on. A dash gives a range: 1-5 in the last field is Monday to Friday. A comma lists separate values.
  5. Step 5: Whose 02:30?. Cron reads the time zone of the machine it runs on. Many servers are set to UTC, so 02:30 there is 03:30 in Paris in winter and 04:30 in summer. On a machine set to local time, the nights when clocks change have no 02:30 at all, or two of them.
  6. Step 6: Overlapping runs. Cron starts a job when the time matches and does not ask whether the last run has finished. A job that starts every 5 minutes but takes 7 runs as two copies for 2 minutes of every 5, which can corrupt shared files. The usual fix is a lock: a run that finds the lock taken exits at once.
  7. Step 7: Missed runs. If the machine is off or cron is stopped at 02:30, that minute passes unseen. Classic cron does not catch up when it comes back: the next backup is a day later. Some schedulers can run a missed job late, and teams often add an alert for a job that has not reported in.
VALUES SO FAR
checksevery minuterule
fields····syntax
expr····example
means····result
server····clock
Paris····local
overlap····risk
missed····default
// BEFORE

Learn this first

Nothing. This concept starts from scratch.

Spotted a mistake on this page?