seuling
seuling

Reputation: 2966

crontab week of the day not working - it send everyday

I use crontab for send email in every first, third Monday.

I tried MON and 1 both, but it worked everyday.

Here's my crontab - I tried both, but both send everyday

0 09 1-7,15-21 * MON
0 09 1-7,15-21 * 1

I think there's no problem with the syntax. What can I do? Thanks in advance.

Upvotes: 1

Views: 762

Answers (3)

Nate Eldredge
Nate Eldredge

Reputation: 58558

This is the correct, documented behavior. From man 5 crontab on Ubuntu 16.04 (emphasis mine):

Note: The day of a command's execution can be specified by two fields — day of month, and day of week. If both fields are restricted (i.e., aren't *), the command will be run when either field matches the current time. For example, 30 4 1,15 * 5 would cause a command to be run at 4:30 am on the 1st and 15th of each month, plus every Friday. One can, however, achieve the desired result by adding a test to the command (see the last example in EXAMPLE CRON FILE below).

Later in the man page they give an example that does just the sort of thing you want:

   # Run on every second Saturday of the month
   0 4 8-14 * *    test $(date +\%u) -eq 6 && echo "2nd Saturday"

Upvotes: 2

seuling
seuling

Reputation: 2966

I found it in man 5 crontab

Note: The day of a command's execution can be specified by two fields — day of month, and day of week. If both fields are restricted (i.e., aren't *), the command will be run when either field matches the current time. For example, ``30 4 1,15 * 5'' would cause a command to be run at 4:30 am on the 1st and 15th of each month, plus every Friday. One can, however, achieve the desired result by adding a test to the command (see the last example in EXAMPLE CRON FILE below).

So it means the day (1-7,15-21) and week of the day (MON) both applied.

When want to specify both day and week of the day, try using date

0 09 1-7,15-21 * $(date +\%Wu) -eq 1 && my_job

Upvotes: 0

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522386

From what I read here, ranges can't wrap around midnight. You might try creating separate CRON jobs for each each possible Monday day, e.g.

0 09 1 * MON
0 09 2 * MON
...
0 09 7 * MON
0 09 15 * MON
0 09 16 * MON
...
0 09 21 * MON

Upvotes: 1

Related Questions