Reputation: 341
I'd love some help on setting up a cron schedule for three scripts I have. I need to schedule the first script to run on the 1st Tuesday of every month, the second script to run on the 2nd Tuesday of every month, and the third script to run on the 3rd Tuesday of every month. Here's what I have so far.
# Run first script on the 1st Tuesday of every month at 9:00 AM
0 9 1,2,3,4,5,6,7 * 2 wget -q -O /dev/null http://site.com/first-script
# Run second script on the 2nd Tuesday of every month at 9:00 AM
0 9 8,9,10,11,12,13,14 * 2 wget -q -O /dev/null http://site.com/second-script
# Run third script on the 3rd Tuesday of every month at 7:00 AM
0 7 15,16,17,18,19,20,21 * 2 wget -q -O /dev/null http://site.com/third-script
I believe these scripts will run on the 1st, 2nd, and 3rd Tuesday of every month AND for every day 1-21 of the month. Looks like from what I've read the day of the week and days are an AND, is that true?
Hopefully this is possible w/ cron, otherwise I'll have to move the decision to run the script or not inside the script itself.
Upvotes: 1
Views: 8512
Reputation: 51
If you don't want to put date checking logic directly into your script, you can have the cron job shell command portion check the day of the week with a condition prior to executing your script.
# Run first script on the 1st Tuesday of every month at 9:00 AM
0 9 1-7 * * [ "$(date '+\%a')" = "Tue" ] && wget -q -O /dev/null http://example.com/first-script
# Run second script on the 2nd Tuesday of every month at 9:00 AM
0 9 8-14 * * [ "$(date '+\%a')" = "Tue" ] && wget -q -O /dev/null http://example.com/second-script
# Run third script on the 3rd Tuesday of every month at 7:00 AM
0 7 15-21 * * [ "$(date '+\%a')" = "Tue" ] && wget -q -O /dev/null http://example.com/third-script
If the [ $(date '+\%a')" = "Tue" ] condition succeeds, the script will execute. The % sign must be escaped with the backslash because cron treats % as a special character.
Because there are 7 days in a week, the first Tuesday of the month is guaranteed to be in the 1-7 range, the second in the 8-14 range, and the third in the 15-21 range.
To capture the first Tuesday, you cannot do:
0 9 1-7 * 2 && wget -q -O /dev/null http://example.com/some-script
...because the 1-7 (Day of Month) and the 2 (Day of Week) will actually be OR'd. Cron treats those two fields differently than the others for some reason. In the example above, your script will end up running every day in the 1-7 range, AND every Tuesday.
Upvotes: 5
Reputation: 301
You can set the cron as:
00 09 1-7,8-14,15-21 * 2 /path/myscript
This will run the script for 9 A.M of 1st, 2nd and 3rd Tuesday.
Upvotes: 2
Reputation: 9
They're an OR.
It will run EVERY TUESDAY AND ON THE DAYS LISTED.
Upvotes: 0
Reputation: 4817
An alternative would be to create main-script to run every Tuesday, verify that Tuesday is and that hour is accordingly invokes the appropriate secondary script.
0 7,9 * * 2 wget -q -O /dev/null http://site.com/main-script
I hope that is helpful.
Greetings.
Upvotes: 2