Developer Tools

Reading a cron expression: five fields and six gotchas

“0 9 * * 1-5” looks like noise. It means 9:00 every weekday. Once you can read the five fields left to right, you can schedule a backup, a report or a reminder in a single line.

By the Toolcloak team8 October 20263 min read
In this article
  1. The five fields
  2. Ten lines you will actually use
  3. The same job, three ways
  4. Six gotchas

Cron is how computers have run things on a schedule since the 1970s: backups, emails, cleanups, reports. The schedule is one line of five fields. It is compact, it is exact, and it is easy to get subtly wrong.

The five fields

Read it left to right
  1. 1Minute0 to 59
  2. 2Hour0 to 23, in 24-hour time
  3. 3Day of month1 to 31
  4. 4Month1 to 12
  5. 5Day of week0 to 6, Sunday is 0 (7 also works in many versions)

Five spaces, five fields. Anything left as * means “every possible value”.

Ten lines you will actually use

ExpressionWhen it runs
* * * * *Every minute
*/15 * * * *Every 15 minutes
0 * * * *At the top of every hour
30 2 * * *Every day at 2:30 am
0 9 * * 1-5At 9:00 am, Monday to Friday
0 0 * * 0Midnight every Sunday
0 0 1 * *Midnight on the 1st of every month
0 8,20 * * *At 8:00 am and 8:00 pm daily
0 0 1 1 *Midnight on 1 January
*/5 9-17 * * 1-5Every 5 minutes between 9:00 and 17:55 on weekdays

Ranges are inclusive: 9-17 includes the 17:xx hour, so the last run is 17:55.

Paste a cron line and it explains it in plain English, with the next run times.Open Cron Expression Explainer

The same job, three ways

“Run a report”: pick the line
  1. 0 9 * * 1-5
    Every weekday morningGood for a daily team summary.
  2. 0 9 * * 1
    Every Monday at 9Good for a weekly digest.
  3. 0 9 1 * *
    The first of each month at 9Good for billing or monthly reports.

Only the last three fields change. Reading cron is mostly noticing where the stars are.

Six gotchas

  1. Time zone. Cron uses the server's clock. A job “at 9” on a UTC server runs at 2:30 pm in India. Check with the date command.
  2. Day of month plus day of week. 0 0 13 * 5 is not “Friday the 13th”. It runs on the 13th and on every Friday.
  3. Every minute by accident. Leaving the minute field as * while setting an hour, as in * 9 * * *, runs 60 times during the 9 o'clock hour.
  4. */n is not “every n from now”.** */45 in the minutes field runs at :00 and :45 every hour, then restarts at :00.
  5. Missed runs. If the server was off at the time, standard cron does not catch up.
  6. Different dialects. Some systems add a seconds field or special names like @daily. Check yours.

Questions people ask

What do the five fields in a cron expression mean?

In order: minute (0-59), hour (0-23), day of month (1-31), month (1-12) and day of week (0-6, Sunday is 0).

How do I run a cron job every 15 minutes?

Use */15 * * * *. It runs at minute 0, 15, 30 and 45 of every hour.

What does 0 9 * * 1-5 mean?

At 9:00 am, Monday through Friday.

Which time zone does cron use?

The time zone of the machine running it, which is often UTC on servers. Convert your intended time before scheduling.

Why did my cron job run on the wrong days?

If both day-of-month and day-of-week are set, standard cron runs the job when either one matches, not only when both do.