Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I want a cronjob to run on every 1st and 3rd Saturday in a month and another one to run every 2nd and 4th Saturday. Now crond offers "step values":

Step values can be used in conjunction with ranges. Following a range with "" specifies skips of the number's value through the range. For example, "0-23/2" can be used in the hours field to specify command execution every other hour (the alternative in the V7 standard is "0,2,4,6,8,10,12,14,16,18,20,22"). Steps are also permitted after an asterisk, so if you want to say "every two hours", just use "*/2".

But if I use

30 3 * * 6/2 command

both scripts run (indeterministically) on every second Saturday. What is the right way to get them to run alternating?

I know the possibility of a wrapper script, but I want to avoid it if there is a more elegant solution.

share|improve this question
add comment

2 Answers

Since you know that one and only one Saturday will occur in the first 7 days of any month, you can follow that pattern and use the day-of-month field to keep each job separate:

30 3 1-7,15-21 * 6 script1
30 3 8-14,22-28 * 6 script2

Thus script1 will always catch the first and third saturdays and script2 will always catch the second and fourth.

share|improve this answer
    
This approach does not work! From the cron man page: "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 (ie, 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." –  d135-1r43 Jun 12 '12 at 7:25
add comment

Why not use the cron #?

30 3 * * 6#1 Firstandthirdcommand
30 3 * * 6#3 Firstandthirdcommand
30 3 * * 6#2 SecondAndfourthCommand
30 3 * * 6#4 SecondAndfourthCommand

Hash ( # ) '#' is allowed for the day-of-week field, and must be followed by a number between one and five. It allows you to specify constructs such as "the second Friday" of a given month

http://en.wikipedia.org/wiki/Cron

share|improve this answer
    
Huh. Interesting. What versions of cron support this? I don't doubt it, and it's a useful concept, but I don't see it in the man 5 crontab of a recent CentOS box, for example. –  cjc Aug 20 '12 at 21:04
    
The only source is the said Wikipedia article where the corresponding paragraph is marked with "citation needed". Are there any other reliable sources? I could not google anything. –  d135-1r43 Aug 21 '12 at 5:58
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.