Cron's day fields don't mean what you think
Set both day of month and day of week and cron runs when either matches. Leave one as * and it's the other way round.
Here is a schedule that looks like "9 am on the first Monday of the month":
0 9 1-7 * 1
It isn't. It runs at 9 am on the 1st to the 7th of every month, and also on every Monday. That is up to eleven runs a month instead of one.
The rule
Standard cron (Vixie cron and cronie, which most Linux systems use) treats the two day fields specially:
- If both day of month and day of week are restricted, the job runs when either matches.
- If either one starts with
*, both have to match, which in practice means only the other field counts.
| Expression | Runs on |
|---|---|
0 9 1 * * |
the 1st of the month |
0 9 * * 1 |
every Monday |
0 9 1 * 1 |
the 1st and every Monday |
0 9 1-7 * 1 |
days 1–7 and every Monday |
You can check any of these in the cron explainer, which flags when both day fields are set.
The */2 trap
The "starts with *" check looks at the text, not the meaning. So */2 in the day-of-month field counts as a star:
0 9 */2 * 1
This runs only on Mondays that fall on an odd-numbered day. Swap */2 for 1-31/2, which selects the same days, and it switches to the either/or rule and runs on every odd day plus every Monday.
Getting "first Monday of the month"
Cron can't express it directly. Run it every Monday and let the command check the date:
0 9 * * 1 [ "$(date +\%d)" -le 7 ] && /usr/local/bin/report
The % has to be escaped as \%, because cron treats a bare % as a newline.
Schedulers with a richer syntax, like Quartz (0 0 9 ? * MON#1), have this built in. Kubernetes CronJobs and GitHub Actions use the standard five fields, so the same rule applies there.