Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

share|improve this question
    
The last monday can range between the 22th (when the 28th February is a Sunday) and the 31th day of the month, so this range is definitely wrong. –  Sven May 3 '11 at 15:54

2 Answers 2

up vote 3 down vote accepted

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.

share|improve this answer
    
Thanks Stephen for your answer, the idea is that we have many scripts running monthly, but we realized that sometimes the run on the weekend, and than, we come on monday bombed with system emails. So, yeah, we could modify all the scripts - but i thought through cronjob it would be smarter... –  Oz123 May 3 '11 at 20:27
    
if monday is too close to the end of the month, we also don't have enough time to audit all the changes we need before the end of the month. Therefore, it is also fine if it the one before last monday, so I will accept you solution, instead of the above solution, which is complicated and does not work... –  Oz123 May 3 '11 at 20:41
1  
For the record: dgrimbergens solution is correct, does indeed work and solves the problem as specified in the question. –  Sven May 4 '11 at 19:14
    
Check my answer for this kind of problems here. stackoverflow.com/questions/3241086/… –  Manuel Gutierrez Mar 31 at 15:07

Your cron job wil run on a Tuesday (5th field, day of week, 0 or 7 is Sunday), and not always the last Tuesday of the month. It would be better to use:

8 04 * * 1 [ $(date +"\%m") -ne $(date -d 7days +"\%m") ] && $HOME/bin/mailstub

This checks if the month today is not equal to the month 7 days from now. If not equal then it's the last monday of the month.

share|improve this answer
    
That's a good idea. –  Sven May 3 '11 at 15:55
    
I have put the date check in the script; putting it in the crontab is clever. –  mpez0 May 3 '11 at 16:10
    
This is really not understood, and it does not work on my system. It gives me an error ... oz123@yenikaro:~$ date Mon May 30 22:45:13 CEST 2011 oz123@yenikaro:~$ $(date +"\%m") -ne $(date -d 7days +"\%m") && echo "yeah" \05: command not found –  Oz123 May 3 '11 at 20:38
1  
It works in the crontab, you tried it on the commandline. If you put the following in your crontab, you can see it work (on 16:00 every day except the last 7 days of the month): 00 16 * * * [ $(date +"\%m") -eq $(date -d 7days +"\%m") ] && echo "it works" >> /tmp/crontab_result –  dgrimbergen May 4 '11 at 14:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.