Cron expressions: a practical guide and the traps hidden in them
Cron syntax fits on an index card, which is why it has lasted since the 1970s. It also has a few behaviours that are not what most people guess, and they tend to show up as a job that ran twice, or not at all, at three in the morning. This guide covers the syntax quickly and the traps properly.
The five fields
A standard cron expression is five fields separated by spaces, followed by the command:
┌───────── minute 0–59
│ ┌─────── hour 0–23
│ │ ┌───── day of month 1–31
│ │ │ ┌─── month 1–12 or JAN–DEC
│ │ │ │ ┌─ day of week 0–6 or SUN–SAT (0 is Sunday)
│ │ │ │ │
* * * * * command
Each field accepts four kinds of value:
*means every value.5means exactly that value.1-5is a range and1,15is a list. They combine:1-5,10.*/15is a step: every 15th value starting from the lowest.10-40/10steps inside a range.
Examples worth memorising
| Expression | Runs |
|---|---|
*/5 * * * * | Every 5 minutes |
0 * * * * | At the start of every hour |
30 2 * * * | Every day at 02:30 |
0 9 * * 1-5 | Weekdays at 09:00 |
0 0 1 * * | Midnight on the first day of each month |
0 */6 * * * | At 00:00, 06:00, 12:00 and 18:00 |
15 3 * * 0 | Sundays at 03:15 |
Read an expression right to left when it is unfamiliar: which days, then which hour, then which minute. A cron parser that lists the next few run times is the quickest way to confirm you wrote what you meant.
Trap 1: day of month and day of week are combined with OR
You would expect 0 0 13 * 5 to mean "midnight on Friday the 13th". In standard cron it means "midnight on the 13th of every month, and also every Friday". When both day fields are restricted (neither is *), a match on either one is enough.
There is no way to express "the 13th, only if it is a Friday" in plain cron. The usual workaround is to schedule on one condition and test the other in the command:
0 0 13 * * [ "$(date +\%u)" = 5 ] && /usr/local/bin/job
The same rule is why "the first Monday of the month" needs a test: schedule 0 9 1-7 * * and check that the day is a Monday inside the command.
Trap 2: steps restart at the top of each period
*/7 in the minute field does not mean "every seven minutes". It means minutes 0, 7, 14, ... 56 of every hour. The gap between :56 and the next :00 is four minutes. Steps only produce an even rhythm when the step divides the range exactly: 1, 2, 3, 4, 5, 6, 10, 12, 15, 20 and 30 for minutes.
The same applies to */5 in the day-of-month field, which runs on the 1st, 6th, 11th ... 31st, then again on the 1st. If you truly need "every 7 minutes" or "every 10 days", cron is the wrong tool; use a scheduler that works with intervals, or have the job record its last run and decide for itself.
Trap 3: which time zone?
Classic cron uses the server's local time zone. A job set for 0 9 * * * runs at 09:00 wherever the machine thinks it is, which is often UTC in the cloud and something else on a laptop. Hosted schedulers differ: some default to UTC, some let you set a zone per job. Always find out, and write the zone next to the expression in a comment.
Daylight saving time makes local-time schedules misbehave twice a year:
- When clocks go forward, an hour does not exist. A job scheduled inside it (for example 02:30) may be skipped, or run just after the jump, depending on the implementation.
- When clocks go back, an hour happens twice, and a job inside it may run twice.
Two habits avoid this: run servers and schedules in UTC, and keep important jobs away from the 01:00–03:00 window if you must use local time. Use a time zone converter to work out what a UTC schedule means for the people affected by it.
Trap 4: overlapping runs
Cron starts a job at its scheduled time whether or not the previous run has finished. A job that normally takes two minutes and is scheduled every five will be fine until the day it takes eight, when two copies run at once and fight over the same files or rows. Wrap long or risky jobs in a lock:
*/5 * * * * flock -n /tmp/sync.lock /usr/local/bin/sync
With -n, a run that cannot get the lock exits at once instead of queuing behind the first.
Trap 5: the environment is not your shell
A command that works when you type it can fail under cron for reasons unrelated to the schedule:
- A minimal
PATH. Cron usually provides little more than/usr/bin:/bin. Use absolute paths or setPATHat the top of the crontab. - No profile. Variables from
.bashrcor.profileare not loaded. - The
%character. In a crontab line an unescaped%ends the command and turns the rest into standard input. Write\%, as in thedate +\%uexample above. - Silent failure. Output goes to local mail that nobody reads. Redirect it to a log:
>> /var/log/job.log 2>&1.
Dialects: five, six and seven fields
Not everything called "cron" uses the same syntax. Know which one you are writing for:
- Unix cron has five fields, as above. Many versions accept both 0 and 7 for Sunday, and shortcuts such as
@hourly,@dailyand@reboot. - Quartz (common in Java systems) puts a seconds field first and an optional year last. It numbers days of the week 1–7 starting from Sunday, and requires a
?in one of the two day fields. It addsL(last),W(nearest weekday) and#(nth weekday), so "first Monday" is0 0 9 ? * MON#1. - Cloud schedulers each have small differences. Some use six fields with a year; some reject expressions that standard cron accepts.
Pasting a five-field expression into a six-field system shifts every value one position to the left, and the result is usually still valid, just wrong. If a job runs at a strange time, count the fields first.
A short checklist
- Which dialect and how many fields?
- Which time zone does the scheduler use, and is it written down?
- If both day fields are set, do you really mean "either"?
- Does the step divide its range evenly?
- What happens if a run is still going when the next one starts?
- Where does the output go, and who finds out when it fails?