Settings

When a crontab line restricts both the day of the month and the day of the week, cron runs the job on any day that matches either field, not only on days that match both. 0 0 13 * 5 runs at midnight on the 13th of every month and on every Friday. The one exception is a day field that starts with *: in Vixie cron and cronie, the cron on most Linux systems, it does not count as restricted, even with a step such as */2, and then both fields must match.

The rule

A crontab line has five time fields: minute, hour, day of the month, month and day of the week. The first four narrow each other: a job runs only in a minute that matches all of them. The two day fields are the exception. The crontab(5) manual says so in a note under the fields: if both are restricted, the command runs when either matches the current time.

The rule is old and deliberate. It lets one line say “on the 1st and 15th, and on Fridays”, which is a reasonable thing to want. It also means there is no way in a single crontab line to say “on a Friday that is the 13th”.

What it looks like

The next runs of each line from the start of 2024, as CRON lists them in UTC:

LineMeantRuns
0 9 1-7 * 19:00 on the first Monday of the monthMon 1 Jan, Tue 2, Wed 3, Thu 4, Fri 5, Sat 6, Sun 7, Mon 8, and every later Monday: days 1 to 7, and every Monday
0 0 13 * 5Midnight on Friday the 13thFri 5 Jan, Fri 12, Sat 13, Fri 19, Fri 26, Fri 2 Feb, Fri 9, Tue 13 Feb: every Friday, and every 13th
0 0 */2 * 1Mondays that fall on an odd dayMon 15 Jan, Mon 29 Jan, Mon 5 Feb, Mon 19 Feb: odd-day Mondays only, because the day of the month starts with *
0 0 1-31 * 1Mondays, written outEvery day: 1-31 is restricted, so it is “any day of the month, or a Monday”

The last two rows are the same days written two ways, and they behave differently. That is the star exception at work: */2 and 1-31 both name days of the month, but only one of them starts with a star.

The star exception

Vixie cron decides whether a day field is restricted by looking at its first character, not at the days it names. A field that begins with * is treated as unrestricted for the either-or rule, so */2, */3 and even */1 count as a star. A field written out as 1-31 names the same days as * and counts as restricted. cronie, the cron that grew out of it on Fedora and Red Hat systems, does the same.

Other schedulers that read the same five fields do not all agree on this corner, so a line that relies on it should be checked against the documentation of whatever will actually run it.

How to run a job on the first Monday of the month

Give cron one of the two day conditions and test the other in the command:

0 9 1-7 * * [ "$(date +\%u)" = 1 ] && /usr/local/bin/report

This runs at 9:00 on each of the first seven days, and the test lets the job through only on the one that is a Monday (date +%u prints 1 for Monday). The backslash before % matters: in a crontab an unescaped % ends the command and the rest of the line is fed to it as input.

The same pattern gives Friday the 13th (0 0 13 * * with a test for 5) and the last day of the month (0 0 28-31 * * with a test that tomorrow is the 1st).

Where the rule is different

  • Quartz (and the schedulers built on it) adds a seconds field and requires a ? in one of the two day fields, so the question of either or both never comes up.
  • systemd timers write the schedule as a calendar expression, such as Mon *-*-01..07 09:00, and a day must match the weekday and the date both. That line is the first Monday of the month.

Checking a line before it goes live

  1. Paste the five fields into CRON. The plain-English line says whether the two days are read as either or both, and why.
  2. Read the list of next runs, in UTC or your own zone, whichever the machine that runs the job uses. A wrong day shows up in the first week.
  3. If the list is longer than you meant, move one of the day conditions into the command as above, and check again.

Questions people ask about cron’s two day fields

Does cron use AND or OR for the day of the month and the day of the week?

OR, when both are restricted: the job runs on a day that matches either field. When one of them is *, the other decides alone. In Vixie cron and cronie a field that starts with *, such as */2, also counts as unrestricted, and then a day has to match both.

How do I schedule a cron job for the first Monday of the month?

Not with the two day fields alone. Use 0 9 1-7 * * and test for Monday in the command: [ "$(date +\%u)" = 1 ] && /path/to/job. Escape the %, which ends the command in a crontab.

Why does 0 0 13 * 5 run every Friday?

Because both day fields are restricted, so cron runs it on the 13th of every month and on every Friday. A single crontab line cannot say Friday the 13th; run it on the 13th and test for Friday in the command.

Is */2 in the day of the month restricted?

For the either-or rule, not in Vixie cron and cronie: they look at the field’s first character, and a field that starts with * counts as unrestricted. So 0 0 */2 * 1 runs only on Mondays that fall on an odd day.

Why does a percent sign break my cron command?

In a crontab, an unescaped % ends the command and everything after it is passed to the command as input, with each later % becoming a new line. Write \% wherever the command itself needs a percent sign, as in date +\%u.

Related tools

More on this

All 8 guides under “Showing the work is yours”.

What your files give away

A one-page checklist of what a photo, a document or a PDF says about you, free now. Then one letter a month, with one new tip.

No spam, unsubscribe at any time, nothing is shared.