Cron to run every 2nd wednesday?
Solution 1:
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
Solution 2:
For CentOS 7 servers, this seems to be the syntax that works for me. Please note the spaces around the [
and ]
. 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
Solution 3:
Based on this answer, you could do:
00 12 * * Wed expr `date +\%d` \> 7 \& `date +\%d` \< 15 >/dev/null && runJob.sh
Solution 4:
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.