11

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

4
  • 1
    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
17

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
3
3

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
2

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.

3
  • according to this: crontab.guru , cron will execute the task every day: 8,9,10,11,...,14 at 4AM. So, it is 7 invocations instead of one. I don't want to test it in production)) – maxkoryukov May 31 '20 at 14:52
  • @maxkoryukov it says "“At 04:00 on every day-of-month from 8 through 14 and on Wednesday.” will execute only once, when its Wed and the day in the range 8-14. – Gabriel A. Zorrilla Jun 8 '20 at 22:04
  • @GabrielA.Zorrilla friend, please check the "next invocations" section. Or run something with this settings on your host. – maxkoryukov Jun 9 '20 at 8:28
1

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
0
1

Based on this answer, you could do:

00 12 * * Wed expr `date +\%d` \> 7 \& `date +\%d` \< 15 >/dev/null && runJob.sh
-1

Fire at 10:15 AM on the third Friday of every month: 0 15 10 ? * 6#3

Source: https://docs.oracle.com/cd/E12058_01/doc/doc.1014/e12030/cron_expressions.htm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy