ToolsWhisper

Cron Expressions Explained: The Five Fields and the Traps in Them

How to read a cron expression, why 0 0 13 * 5 is not Friday the 13th, why */7 is uneven, and the time zone and escaping mistakes that break scheduled jobs.

Frank4 min read

A cron expression is five fields of text that say when a job should run. They are compact enough to be unreadable, and just irregular enough that confident people get them wrong. The usual result is a job that runs at the wrong time, runs far too often, or never runs at all, and nothing in the schedule itself tells you.

The five fields

┌───────── minute        (0–59)
│ ┌─────── hour          (0–23)
│ │ ┌───── day of month  (1–31)
│ │ │ ┌─── month         (1–12)
│ │ │ │ ┌─ day of week   (0–6, Sunday is 0; 7 also means Sunday in most implementations)
│ │ │ │ │
* * * * *   command

Each field accepts four kinds of value:

Syntax Meaning Example
* Every value * in the hour field is every hour
5 One specific value 5 in the hour field is 05:00
1,15 A list 1,15 in day of month is the 1st and 15th
9-17 A range 9-17 in hour is 09:00 through 17:00
*/10 A step */10 in minute is every 10 minutes

So 30 6 * * 1-5 means 06:30, Monday to Friday. The cron expression generator reads any expression back in plain English and lists the next run times, which is the fastest way to check a schedule before it goes live.

Trap 1: "Friday the 13th" runs on every Friday

You might expect 0 0 13 * 5 to run at midnight on Friday the 13th. It does not. When both the day-of-month and day-of-week fields are restricted, cron runs the job when either one matches. This schedule runs at midnight on the 13th of every month and at midnight every Friday.

This is documented behaviour, not a bug. The POSIX specification for crontab states that if both fields are given as elements or lists, a day matching either one is matched. If you genuinely need Friday the 13th, schedule the job for every Friday and have the script check the date itself.

Trap 2: steps do not divide evenly

*/5 in the minute field means every 5 minutes, which divides an hour cleanly. */7 does not. A step counts up from the start of the range, so */7 in minutes fires at:

0, 7, 14, 21, 28, 35, 42, 49, 56

and then the next hour begins at minute 0, only 4 minutes after the last one. The gaps are 7, 7, 7, 7, 7, 7, 7, 7 and then 4.

The same thing happens in other fields. 0 */5 * * * runs at hours 0, 5, 10, 15 and 20, and then again at 0, so the gap across midnight is 4 hours. Pick steps that divide the range exactly (for minutes, 5, 10, 15, 20, 30), or accept the uneven spacing deliberately.

Trap 3: time zones

A cron daemon uses the clock of the machine it runs on, and that machine may not be in the zone you are thinking in. A server in a data centre is often set to UTC, so 0 9 * * * fires at 09:00 UTC, not 09:00 where you are.

Hosted schedulers make their own choices. GitHub Actions, for example, runs scheduled workflows in UTC by default, and its documentation notes that the shortest interval it supports is once every 5 minutes. Check what your platform does rather than assuming.

Daylight saving makes it worse. When clocks spring forward, a job set for a time inside the skipped hour may not run that day, and when they fall back, a time in the repeated hour can occur twice. Behaviour varies between implementations. If a job must run exactly once per day, schedule it for a time that is never skipped or repeated, or run the machine in UTC.

The generator shows upcoming run times in your browser's own time zone. Compare them with the zone your scheduler actually uses.

Trap 4: the crontab line is not a shell prompt

Even a correct schedule fails if the command is wrong, and the environment cron gives a job is thin.

  • A minimal PATH. A command that works in your terminal may not be found. Use absolute paths to programs and files.
  • % is special. In many crontab implementations an unescaped % in the command is treated as a newline. A date +%Y-%m-%d in a command usually has to be written as date +\%Y-\%m-\%d.
  • Output goes nowhere useful. Redirect it explicitly, such as >> /var/log/job.log 2>&1, or the first sign of a failure will be silence.
  • Overlap. If a job takes longer than its interval, the next run starts while the previous one is still going. Use a lock (for example flock) when that would be harmful.

Not every scheduler uses five fields

Standard cron has five fields. Some systems add a seconds field, a year field, or a ? placeholder for "no specific value", and the exact syntax differs between them. If an expression copied from one platform looks like it has six or seven fields, it is not standard cron and will not mean the same thing elsewhere. The generator expects exactly five fields and tells you when it does not get them.

A habit worth keeping

Before saving any schedule, read it back in words and check the next five run times against what you intended. It takes ten seconds, and it is how you find out that "every 7 minutes" has a 4-minute gap in it. Timing problems often turn out to be timestamp problems too. If the job writes or compares times, the Unix timestamp converter is useful for checking what a stored value actually means.

Tools used in this article

Read next