1826116 Members
4599 Online
109690 Solutions
New Discussion

Re: Cron jon schedule

 
SOLVED
Go to solution
Fuad_1
Regular Advisor

Cron jon schedule

Hi,

Can I schedule a cron job to start every last Friday of each month?

Thanks.
Set goals, and work to achieve them
4 REPLIES 4
Peter Godron
Honored Contributor

Re: Cron jon schedule

Hi,
crontab can't do it directly. However,for solution, see:
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=371092



Peter Godron
Honored Contributor
Solution

Re: Cron jon schedule

Fuad,
schedule your job to run every Friday and include this at the start of your code.

thanks to Cem Tugrul:

## get actual day
DAY=`date +"%d"`
## get the last friday of curren month
EXEC_DAY=`cal | awk 'NF >=6 { last = $6 }; END{ print last }'`

if [ $DAY -ne $EXEC_DAY ]
then
echo " - Not run because is not the last friday - "
exit;
fi
A. Clay Stephenson
Acclaimed Contributor

Re: Cron jon schedule

Cron can't do this itself so one technique is to execute the cron script EVERY Friday and then let the script decide if it should do something or simply exit.

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

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

if [[ $(caljd.sh -M) -ne $(caljd.sh -M -n 7) ]]
then
echo "It's the last Friday; do your thing"
# run your command and set ${STAT} approriately to indicate exit status
else
echo "It ain't; do nothing"
fi
exit ${STAT}

----------------------------------------

Install the attached caljd.sh in /usr/local/bin and invoke a caljd.sh -u for full usage and examples. Basically the idea is that we compare the month of the current date and the month 1 week later to see if they differ.

If it ain't broke, I can fix that.
Fuad_1
Regular Advisor

Re: Cron jon schedule

Thanks to all.
Set goals, and work to achieve them