Cron – How to Set Up a Cron Job That Runs on the Last Monday of the Month

cron

I need launch a few cron scripts on the last monday of every month.

I was wondering if combining ranges will do the job, e.g.:

8 04 19-26  *  2     $HOME/bin/mailstub

I hope I got it right and that my cron job will only run ONCE in the week between 19 and 26 of every month.

Would be happy to hear peoples comments,

I am using CentOS 5.6 – so the L construct mentioned in some places (e.g. Wikipedia) does not work on my system.

Thanks in advance,

Oz

Best Answer

That is setup to go multiple times per day. You want:

0 8 04 19-26 * * MON $HOME/bin/mailstub

That will go off at 8:04:00 on any day between the 19-26 that is on a Monday of any month of any year.

However, if it is Monday on the 19th, it is monday on the 26th. Therefore, you would want to have it from the 19-25

0 8 04 19-25 * * MON $HOME/bin/mailstub

So, this should go off once per month on a Monday. However, it is not guarenteed (or likely) to be the last Monday. If you need it to be the last and the L syntax does not work, you need to program the logic into the script being called. This is easily done with python but is beyond the scope of this question.