crontab 4th Tuesday of month

Jorge Fábregas jorge.fabregas at gmail.com
Sat Jul 28 12:50:15 UTC 2012


On 07/28/2012 07:03 AM, Frank Murphy wrote:
> I thought the last tuesday would be covered by above.
> But it has run everyday since 22nd.

Hi,

There's no way to accomplish this via crontab itself.  You'll need a
little help by means of a script.

This works:

1)  create the following script. Let's call it tuesday-check.sh:

#/bin/sh
[ `date +%m` -ne `date -d "+7 days" +%m` ] && your-actual-command.sh


2) create a crontab entry for the above script so that it runs every
Tuesday (yes I know you want the last Tuesday;  the script will
precisely check that):

15 18 * * 2 /root/bin/tuesday-check.sh


Explanation:

date +%m
...returns the *month number* for today

date -d "+7 days" +%m
...returns the "month number" for this day *one week ahead*


For example, if I were to run this on the last Saturday of the month
(today the 28th):

- the first command would return "7".
- the second command would return "8" because the NEXT Saturday is
already August.

Because both of the returned numbers are different (7 and 8) the command
will run.

HTH,
Jorge


More information about the users mailing list