Guide

Cron expressions: a practical guide and the traps hidden in them

Published · 8 minute read

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:

Examples worth memorising

ExpressionRuns
*/5 * * * *Every 5 minutes
0 * * * *At the start of every hour
30 2 * * *Every day at 02:30
0 9 * * 1-5Weekdays 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 * * 0Sundays 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:

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:

Dialects: five, six and seven fields

Not everything called "cron" uses the same syntax. Know which one you are writing for:

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

  1. Which dialect and how many fields?
  2. Which time zone does the scheduler use, and is it written down?
  3. If both day fields are set, do you really mean "either"?
  4. Does the step divide its range evenly?
  5. What happens if a run is still going when the next one starts?
  6. Where does the output go, and who finds out when it fails?

Tools used in this guide

More guides