I need to find a way to cron a job so that it runs every second wednesday of the month. Is this possible?
For CentOS 7 servers, this seems to be the syntax that works for me. Please note the spaces around the
]. That took a while for me to figure out.
This runs the
test.sh file at 13:07 / 1:07PM on the second Wednesday of the month.
(0=Sunday, 1=Monday, 2=Tuesday, 3=Wednesday, etc.)
07 13 8-14 * * [ `date +\%u` = 3 ] && /root/scripts/test.sh
Based on this answer, you could do:
00 12 * * Wed expr `date +\%d` \> 7 \& `date +\%d` \< 15 >/dev/null && runJob.sh
You can avoid running an external script by using a combination of the day of week trick plus the weekday:
# Run on every second Wednesday of the month 0 4 8-14 * Wed job.sh
This also avoids running another external program. The 8-14 selects all days that match the second week of the month. Then it filters out just that Wednesday.
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
#!/usr/bin/env bash #get day of month day_of_month=`date +%d` #if this day is between 7th and 15th day of the month = 2nd week if [ $day_of_month -gt 7 -a $day_of_month -lt 15 ]; then # Call your program here instead of 'ls'… ls fi
Fire at 10:15 AM on the third Friday of every month:
0 15 10 ? * 6#3