Operating System - HP-UX
1820498 Members
3602 Online
109625 Solutions
New Discussion юеВ

Setup a cron job to be run on 1st monday of every month

 
sapan
Occasional Contributor

Setup a cron job to be run on 1st monday of every month

Guys,

How to setup acron job , which runs on 1st monday of every month at 10 AM.

I have thought something like this

00 10 1-7 * 1

but as per crontab man page , if you specify
day and dow both , result will be cumulative , i.e it will do both .

so i am afraid if it runs on 1-7 and every monday of all months.
5 REPLIES 5
James R. Ferguson
Acclaimed Contributor

Re: Setup a cron job to be run on 1st monday of every month

Hi:

Let your script determine if the date falls within the first week. Simply create your crontask to run at 1000 on _every_ Monday and within your script do:

...
[ $(date +%d) -gt 7 ] && exit
...

Regards!

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: Setup a cron job to be run on 1st monday of every month

When you specify both the days of the month and a weekdays value, cron does a logical OR rather than a logical AND that you expected. This is documented behavior and is normal. As mentioned, the correct approach is to specify only one of these and then let the cron'ed script decide if this is the first monday. You could turn the world around and run your script every 1-7 of the month and then use the date command to determine if today is Monday.
If it ain't broke, I can fix that.
rajdev
Valued Contributor

Re: Setup a cron job to be run on 1st monday of every month

Hi Sapan,

yes this is the cron limitation, so as JRF specified, the script should be used to further determine if the day is < 7 ...

Regards,
RD
Dennis Handly
Acclaimed Contributor

Re: Setup a cron job to be run on 1st monday of every month

Some other threads with the same topics:
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=1156357
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=1153076
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=1096366
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=1131448

As Clay said, it is better to check the day of week in your script. You might want to have a comment in your cron file that notes that.

The thread with 1096366 shows you how to check for the day of week no matter what locale.

>RD: this is the cron limitation,

(More like the cron standard.)
Jim Walls
Trusted Contributor

Re: Setup a cron job to be run on 1st monday of every month

Another solution that I have found to be useful is to invoke an "at" job.

Your cron line could look something like this:

0 0 1 * * at -f script 10:00 Monday 2> dev/null

This will execute the "at" job on the next occurrence of 10am on Monday ... the "script" can be any file of commands or a shell script or even a perl script.

I use this mechanism, for example, to schedule Ignite backups to run over the first weekend of the month.