Why `0 9 1 * 1` Is Not the First Monday of the Month
If you have ever shipped a crontab line that looked obviously correct and then watched the job fire on days you never intended, you have probably met cron's most unintuitive rule. It is not a parser bug. It is how standard 5-field cron has worked for decades, and it still surprises people who schedule production jobs.
The expression everyone thinks they understand
A normal crontab entry has five fields:
minute hour day-of-month month day-of-week So this looks like "09:00 on the first of the month, on Monday":
0 9 1 * 1 Read it in English and you will almost certainly hear one of two intents:
- Run only when the first of the month is a Monday.
- Run on the first Monday of the month.
Standard cron does neither.
When both the day-of-month and the day-of-week fields are restricted — meaning neither is * — the job runs if either field matches. The two day fields are combined with OR, not AND. Minute, hour, and month still all have to match, as usual.
So 0 9 1 * 1 means:
- 09:00 on the 1st of every month, and
- 09:00 every Monday.
If you wanted a single monthly run, you will get four or five extra runs every month. If the 1st happens to fall on a Monday, you still only get one run that morning — but that is coincidence, not "first Monday" logic.
A concrete month
Take December 2026. The 1st is a Tuesday. Mondays fall on the 7th, 14th, 21st, and 28th.
0 9 1 * 1 therefore fires at 09:00 on:
- Tuesday, 1 December 2026 (day-of-month matched)
- Monday, 7 December 2026 (day-of-week matched)
- Monday, 14 December 2026
- Monday, 21 December 2026
- Monday, 28 December 2026
Five mornings, not one. The first Monday of the month is the 7th, and the job does run that day — along with every other Monday, and the 1st besides.
January 2027 follows the same pattern with different dates: the 1st is a Friday, so you get 1 January plus the Mondays (4, 11, 18, and 25).
Why this trips people
The rule is easy to miss for three reasons.
First, natural language lies. "The 1st on Monday" sounds like a single, tighter constraint. Cron is not parsing a sentence. It is matching fields, and the two day fields have a special case.
Second, the special case only kicks in when both day fields are restricted. These two expressions behave the way most people expect:
0 9 1 * * # 09:00 on the 1st of every month 0 9 * * 1 # 09:00 every Monday In each of those, one day field is *, so there is no OR. The remaining day constraint must match. The surprise appears only when you fill in both day fields at once.
Third, a popular "fix" is also wrong on standard cron. You will see this suggested for "the first Monday of the month":
0 9 1-7 * 1 The idea is: only consider the first seven days, and only Monday. Because both day fields are still restricted, standard cron treats this as OR again. The job runs on every day from the 1st through the 7th, and on every Monday in the month. That is worse, not better.
What to write instead
Decide which of these you actually want.
A monthly date, regardless of weekday. Leave day-of-week as *:
0 9 1 * * A weekday, regardless of the date. Leave day-of-month as *:
0 9 * * 1 The 1st, but only when it is a Monday — or the first Monday of the month. Standard 5-field cron cannot express either of those by itself. Schedule the broader case and guard it in the job.
For the first Monday:
0 9 * * 1 # only the first Monday if [ "$(date +%d)" -gt 7 ]; then exit 0 fi # ... real work The job is offered every Monday and no-ops unless the date is 1–7, which is exactly the first Monday. For "the 1st only if it is Monday," schedule 0 9 1 * * and check the weekday inside the script instead.
One caveat: not every scheduler follows this rule. Traditional crontab and Kubernetes CronJobs use the OR behavior described above when both day fields are restricted. Quartz-style cron — common in Spring, and close to what AWS EventBridge uses — usually requires ? in one of the two day fields and will not treat a fully specified pair as OR. If you are copying an expression from a Linux box into a Java scheduler, or the other way around, read that system's docs before you assume the calendar will look the same.
Check the timestamps before you deploy
The reliable way to catch this is not to reread the expression. It is to list the next fire times and look at the dates.
Paste the line into a calculator that lists upcoming run times, set a start datetime if you want to inspect a particular month, and read the results in both local time and UTC. If you see a run on the 1st and another a few days later on a Monday, the OR rule is doing what it always does — and you can fix the expression or add the guard before the job ever hits production.
Names like MON and JAN, ranges, lists, and steps (*/15, 0-30/10) are all worth checking the same way, especially around month boundaries and time zones. A job that looks right in your head can still be an hour off once you compare local time to UTC.
Valid syntax is not the same thing as the schedule you meant. For cron, the difference usually shows up on the calendar, not in a parser error.
原文:https://www.a8q9.cn/en/article/why-0-9-1-1-is-not-the-first-monday-of-the-month-772e7f.html