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

I need to find a way to cron a job so that it runs every second wednesday of the month. Is this possible?

share|improve this question

migrated from stackoverflow.com Jun 20 '11 at 15:21

This question came from our site for professional and enthusiast programmers.

    
This question looks to provide an appropriate answer. –  scurker Jun 20 '11 at 14:16
1  
Yeah, but that question's about Tue, not Wed ;) The accepted answer is clever, though. –  edoloughlin Jun 20 '11 at 14:22
    
>> Yeah, but that question's about Tue, not Wed ;) The accepted answer is clever, though ... what is so hard to change Tue to Wed ? –  ajreal Jun 20 '11 at 14:33
    
0 * * * 3 test $(date \+%u) -eq 3 && echo "start run me" try this. didn't paste to the answer because one liner question is pretty vague. –  Jasonw Jun 20 '11 at 14:53

3 Answers 3

My manpage for crontab (which I sadly can't seem to find online) gives the following example:

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

Adapting this to your purposes...

0 4 8-14 * *    test $(date +%u) -eq 3 && job.sh
share|improve this answer
    
To anyone looking to edit this answer, the reason you don't just use the day of week field is that if both day of month and day of week are restricted (ie, aren't *), the command will run when either one matches. –  Hasturkun Jan 24 '13 at 18:51
    

Based on this answer, you could do:

00 12 * * Wed expr `date +\%d` \> 7 \& `date +\%d` \< 15 >/dev/null && runJob.sh
share|improve this answer

It's not possible using cron on its own, but you could call a script once a week that does the test:

In crontab, run second_wed.sh at 12.00 every Wednesday:

0 12 * * 3 /home/you/bin/second_wed.sh

In second_wed.sh:

#!/usr/bin/env bash
day_of_month=`date +"%
if [ $day_of_month -gt 7 -a $day_of_month -lt 15 ]; then
  # Call your program here instead of 'ls'…
  ls
fi
share|improve this answer
    
Wouldn't it be a bit more straight forward to use the fifth field (day of week)? No need to call it on the other days. –  Fredrik Jun 20 '11 at 14:25

Your Answer

 
discard

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