Re: cron job that runs second Sunday of every month (2246 Views)
Reply
Occasional Visitor
query
Posts: 1
Registered: ‎08-14-2007
Message 1 of 5 (2,246 Views)

cron job that runs second sunday of every month

Hi all,

May I know how to run a cron job that runs on every second sunday of every month.

Thanx in advance :)
Honored Contributor
Bill Hassell
Posts: 14,225
Registered: ‎05-29-2000
Message 2 of 5 (2,246 Views)

Re: cron job that runs second sunday of every month

cron cannot do this by itself. You can run your script every Sunday using cron. Then at the start of your script, test for the day of the month. If it is between 8 and 14, then continue with the script, otherwise exit:

#!/usr/bin/sh
set -u
DAY= $(date "+%d")
[ $DAY -ge 8 -a $DAY -le 14 ] || exit

... rest of your script ...

Then in cron, the entry would be:

1 1 * * 0 /somepath/myscript

where 1 1 = minute and hour to run
* * = day and month don't matter
0 = only on Sunday

Your script runs every Sunday at 01:01 in the morning, but immediately exits in the day is not 8 through 14.
Acclaimed Contributor
A. Clay Stephenson
Posts: 17,825
Registered: ‎07-16-1998
Message 3 of 5 (2,246 Views)

Re: cron job that runs second sunday of every month

Cron ain't that smart on its own but one approach is to make use of the attached script, caljd.sh, that will do just about any date function you like. Install it in whatever directory you like and make it executable then your cron'ed script which should be called every Sunday should look something like this:

---------------------------------------------
#!/usr/bin/sh

export PATH=${PATH}:/usr/local/bin
typeset -i STAT=0

if [[ $(caljd.sh -N) -eq 2 ]]
then
echo "It's the 2nd Sunday; do your thing"
STAT=${?}
fi
exit ${STAT}
-------------------------------------------
Invoke as caljd.sh -u for full usage and many examples.
If it ain't broke, I can fix that.
Acclaimed Contributor
A. Clay Stephenson
Posts: 17,825
Registered: ‎07-16-1998
Message 4 of 5 (2,246 Views)

Re: cron job that runs second sunday of every month

Cron ain't that smart on its own but one approach is to make use of the attached script, caljd.sh, that will do just about any date function you like. Install it in whatever directory you like and make it executable then your cron'ed script which should be called every Sunday should look something like this:

---------------------------------------------
#!/usr/bin/sh

export PATH=${PATH}:/usr/local/bin
typeset -i STAT=0

if [[ $(caljd.sh -N) -eq 2 ]]
then
echo "It's the 2nd Sunday; do your thing"
STAT=${?}
fi
exit ${STAT}
-------------------------------------------
Invoke as caljd.sh -u for full usage and many examples.
If it ain't broke, I can fix that.
Acclaimed Contributor
Dennis Handly
Posts: 25,290
Registered: ‎03-06-2006
Message 5 of 5 (2,246 Views)

Re: cron job that runs second Sunday of every month

[ Edited ]

Here are two other threads that want the first Sunday. My suggestion was to do it on the date range of 8-14 and then your script only has to check for Sunday.
http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1096366
http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1131448

The reason it isn't directly possible in cron is that it ORs the day of month range with the day of week. (With tzab, it does an AND.)

The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.