Operating System - Linux
1748089 Members
4889 Online
108758 Solutions
New Discussion юеВ

Re: cron job that runs second Sunday of every month

 
query
New Member

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 :)
4 REPLIES 4
Bill Hassell
Honored Contributor

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.


Bill Hassell, sysadmin
A. Clay Stephenson
Acclaimed Contributor

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.
A. Clay Stephenson
Acclaimed Contributor

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.
Dennis Handly
Acclaimed Contributor

Re: cron job that runs second Sunday of every month

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.)