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.
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
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 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
Try it yourself
Learn this first
Nothing. This concept starts from scratch.
Learn next
- Background jobs and workersMove slow work out of the request: a queue, workers and retries. Beginner
- Data pipelines and orchestrationA nightly report as a graph of tasks: scheduled, retried and backfilled. Beginner
- Backups and point-in-time recoveryRewind a database to the minute before a mistake, using backups and its log. Intermediate
Spotted a mistake on this page?