If you set both the day-of-month and the day-of-week field in a crontab line, the job runs when either one matches, not when both do. So this line does not mean "Friday the 13th":

0 9 13 * 5 /usr/local/bin/spooky.sh

It runs at 09:00 on every 13th of the month, and also at 09:00 on every Friday. That is the behaviour of Vixie cron and its descendants (cronie on RHEL, Fedora and Amazon Linux, and the cron package on Debian and Ubuntu), and it's documented in man 5 crontab. It surprises almost everyone the first time.

The exact rule

The man page puts it this way: if both fields are restricted (that is, they don't start with *), the command runs when either field matches the current time.

Day of monthDay of weekResult
**Every day
13*Only on the 13th
*5Only on Fridays
135The 13th or any Friday

When one field is *, it matches everything, so the other one decides. When both are restricted, the implementation can't intersect them without changing what the original authors wrote, so it takes the union.

More examples of the trap

# Intended: 1st of the month, only if it's a Monday
# Actual: every 1st AND every Monday
0 8 1 * 1 /opt/scripts/monthly.sh

# Intended: the first Monday of the month (days 1-7 that are Monday)
# Actual: days 1-7 of every month AND every Monday
0 8 1-7 * 1 /opt/scripts/first-monday.sh

# Intended: weekdays that fall on the 15th
# Actual: every 15th AND every weekday
0 8 15 * 1-5 /opt/scripts/payday.sh

The 1-7 plus Monday pattern is the one you'll find copied around the internet. It looks right and does the wrong thing: it runs on days 1 to 7 and on every other Monday of the month.

A leading asterisk changes the rule

In Vixie cron and cronie, the OR behaviour only applies when neither field starts with *. The implementation checks the first character of each field. A field that begins with * is treated as "unrestricted" for this purpose, even when it has a step:

# Day-of-week field is */2, so it starts with *.
# The two fields are ANDed: the 13th, and only if it's a Sunday, Tuesday, Thursday or Saturday.
0 9 13 * */2 /usr/local/bin/check.sh

So */2, */3 and similar in either field switch the rule from OR to AND. This is a quirk of how the code is written, not something worth relying on in a crontab other people maintain. If you use it, leave a comment. Other implementations (BusyBox crond, some Go and Python libraries, systemd calendar expressions) don't necessarily agree, so test on the system that will actually run the job.

Getting AND behaviour with a test in the command

The portable way to say "this day of the month and this weekday" is to restrict one field in the schedule and check the other in the command.

Friday the 13th, using a date test:

0 9 13 * * [ "$(date +\%u)" = 5 ] && /usr/local/bin/spooky.sh

%u is the day of the week with Monday as 1 and Sunday as 7, so 5 is Friday. Inside a crontab line the % must be escaped as \%, otherwise cron turns it into a newline. Details on that in why a cron job doesn't run.

If the check lives inside a script, the escaping goes away:

#!/bin/sh
# run-if-friday.sh
[ "$(date +%u)" = 5 ] || exit 0
exec /usr/local/bin/spooky.sh

First Monday of the month

Schedule it for every Monday and let the command skip all but the first one. The first Monday always falls on day 1 to 7:

0 8 * * 1 [ "$(date +\%d)" -le 7 ] && /opt/scripts/first-monday.sh

Change 1 to the weekday you need. For the second occurrence use -gt 7 -a ... -le 14:

0 8 * * 1 [ "$(date +\%d)" -gt 7 ] && [ "$(date +\%d)" -le 14 ] && /opt/scripts/second-monday.sh

Last day of the month

Cron has no "last day" token, but you can run on days 28 to 31 and check whether tomorrow is the 1st. This uses GNU date:

0 23 28-31 * * [ "$(date -d tomorrow +\%d)" = 01 ] && /opt/scripts/month-end.sh

On macOS and BSD, the equivalent is date -v+1d +%d.

Last Friday of the month

Run every Friday and check that seven days from now is in a different month:

0 18 * * 5 [ "$(date -d '+7 days' +\%m)" != "$(date +\%m)" ] && /opt/scripts/last-friday.sh

Last weekday of the month

Run Monday to Friday on days 28 to 31 only if the next weekday is in a new month. This one is easier in a script than in a crontab line:

#!/bin/sh
# Exit unless today is the last weekday (Mon-Fri) of the month
next=$(date -d 'tomorrow' +%u)
# If tomorrow is Saturday (6) or Sunday (7), look to the next Monday
case "$next" in
  6) next_date=$(date -d '+3 days' +%m) ;;
  7) next_date=$(date -d '+2 days' +%m) ;;
  *) next_date=$(date -d '+1 day' +%m) ;;
esac
[ "$next_date" != "$(date +%m)" ] || exit 0
exec /opt/scripts/month-end.sh

Schedule it with 0 18 * * 1-5 /opt/scripts/last-weekday.sh. The -d flag requires GNU date, which Linux distributions ship by default.

How Quartz and AWS avoid the problem

Quartz and Amazon EventBridge dodge the whole question by refusing to let you restrict both fields. One of the two must be ?, which means "no specific value":

# Quartz: 6-7 fields, with seconds
0 0 9 13 * ?        # 09:00 on the 13th, any weekday
0 0 9 ? * 6         # 09:00 every Friday (Quartz: 1 = Sunday)
0 0 9 ? * 2#1       # 09:00 on the first Monday of the month
0 0 9 ? * 6L        # 09:00 on the last Friday of the month
0 0 9 L * ?         # 09:00 on the last day of the month

# AWS EventBridge: 6 fields, with year
cron(0 9 ? * 2#1 *)   # first Monday of the month
cron(0 9 L * ? *)     # last day of the month

These systems also have built-in tokens (#, L, W) that cron lacks, so "first Monday" and "last day of the month" don't need a shell test. If you're moving schedules between systems, expect to rewrite these lines. See the Quartz guide and the AWS EventBridge guide for the full syntax. Kubernetes CronJobs follow the Unix rule, so the OR behaviour applies there too.

Checking what your schedule really does

When you can't tell whether a line will behave as OR or AND, paste it into the CronGen decoder. It warns when both day fields are restricted so you can spot the trap before the job misfires on a day nobody expected. For a cron daemon itself, the real test is to look at the log after the first scheduled day.

A short checklist:

  • Both day fields set and neither starts with *: it's OR.
  • You want AND: restrict one field, test the other in the command, and escape %.
  • The command is complicated: put the date check in a script rather than in the crontab line.
  • You depend on a */n field to force AND: add a comment, or use the explicit test instead.