Operating System - HP-UX
1819791 Members
3218 Online
109607 Solutions
New Discussion юеВ

How to make cron skip holidays

 
SOLVED
Go to solution

How to make cron skip holidays

Hello all,

I have some routine tasks that are run each weekday by cron. This Friday is a company holiday so nobody will be here to change tapes. Is there a way to make cron skip holidays?

TIA, Steve
12 REPLIES 12
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: How to make cron skip holidays

Cron won't do it on its own but you can determine this within your script. You would let your command run everyday as usual but add something like this to the script:

if [[ ${caljd.sh -h) -ne $(caljd.sh) ]]
then
echo "Today is a holiday."
exit 0
fi

You will need to create/update a seldom used anymore file called /etc/acct/holidays.

If it ain't broke, I can fix that.
Geoff Wild
Honored Contributor

Re: How to make cron skip holidays

Not really - but you could write an elaborate scheme whereby you resubmit cron dailey. The script would check a calendar file - if it is a holiday - then load cron.holiday, if a work day, then load cron.workday, etc.

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
A. Clay Stephenson
Acclaimed Contributor

Re: How to make cron skip holidays

I suppose I had better attach the caljd.sh script although a casual search of the Forums should turn up a herd of hits as well as its Perl cousin, caljd.pl.

You can invoke caljd.sh with -u for full usage. The -h checks for holidays and if true advances the Julian Day by one unless the next day also happens to be a holiday in which case the current Julian Day is incremented again.


If it ain't broke, I can fix that.
Zafar A. Mohammed_1
Trusted Contributor

Re: How to make cron skip holidays

Dario_1
Trusted Contributor

Re: How to make cron skip holidays

Hi!

You won't be able to do that from cron. You will have to modify your script to do it.

Regards,

DR
Donny Jekels
Respected Contributor

Re: How to make cron skip holidays

you can't to it in cron. but put your scritp in cron to run everyday.

then put this inside your side.

check_date()
{
#
# check if it is the alternative thursday?
#
if [ $DATE = 9 ] || [ $DATE = 23 ] || [ $DATE = 37 ] || [ $DATE = 51 ] || [ $DATE = 65 ] || 50 [ $DATE = 79 ] || [ $DATE = 93 ] || [ $DATE = 107 ] || [ $DATE = 121 ] || [ $DATE = 135 ] || 51 [ $DATE = 149 ] || [ $DATE = 163 ] || [ $DATE = 177 ] || [ $DATE = 191 ] || [ $DATE = 205 ] || 52 [ $DATE = 219 ] || [ $DATE = 233 ] || [ $DATE = 247 ] || [ $DATE = 261 ] || [ $DATE = 275 ] || 53 [ $DATE = 289 ] || [ $DATE = 303 ] || [ $DATE = 317 ] || [ $DATE = 331 ] || [ $DATE = 345 ] || [ $DATE = 359 ]
then
# send the email, else forget about it

EXECUTE anything you want done if it is the day you want the job to run
}


this code goes in your main (ksh)
BIN=/usr/local/bin
DT=`date +%D`
echo "$DT\nq\n" | $BIN/date2julian | awk '{print $9}' | read DATE
echo "$DT" | sed "s/\// /gp" | read M D Y
(( YESTERDAY = $DATE - 1 ))
echo "$YESTERDAY\nq\n" | $BIN/date2julian | awk '{print $7}' | read ETAD
echo "$ETAD" | sed "s/\// /gp" | read MM DD YY

check_date



put the attachment in your /usr/local/bin directory, make it executable.


enjoy
Donny
"Vision, is the art of seeing the invisible"
Donny Jekels
Respected Contributor

Re: How to make cron skip holidays

I use this excell spread sheet to determin which days I want the job to run. then I use the julian date inside my script.

also, i made a type in the previous posting.

the if statement in the function check_date() must have a fi in the line before the
check_date()
{
if [ $DATE = 5 ] || [ $DATE = 28 ]
then
do something
else
forget about it, and try again tomorrow, hahaha
fi
}

you can also omit the else and go strait to the
check_date()
{
if [ $DATE = 325 ]
then
echo "almost x-mas"
fi
}


peace
Donny
"Vision, is the art of seeing the invisible"
Donny Jekels
Respected Contributor

Re: How to make cron skip holidays

my date2julian script is only 114 lines long.

the caljd.sh and it perl cousin is over 1090
line long. :o(

date2julian even knows and can calculate leap years. once you implement it. you can forget it forever, okay until the date runs over 3000.

I use it in every single script. nice to step back for backup jobs, when cron can't check if its month end.

date2julian you can go crazy, stepping back and forth in time, without skipping a day.
"Vision, is the art of seeing the invisible"

Re: How to make cron skip holidays

Hi A. Clay,

I used your script inside my script and it works great if I run it from the shell but fails if I run it from cron. What's wrong?

TIA, Steve
A. Clay Stephenson
Acclaimed Contributor

Re: How to make cron skip holidays

Hi Steve:

Your problem is very typical. Cron has an intentionally sparse environment. You need to do something like early in your cron'ed script:

PATH=${PATH}:/usr/local/bin
export PATH

assuming that you installed caljd.sh in /usr/local/bin. Plan B. use full paths when invoking caljd.sh (or anything else).

This should fix you - if you get /etc/acct/holidays set correctly.
If it ain't broke, I can fix that.

Re: How to make cron skip holidays

Thanks A. Clay. That fixed it. I did a search for "caljd.sh" and got dozens of hits. That is some script!!!

Thanks, Steve
Steven E. Protter
Exalted Contributor

Re: How to make cron skip holidays

I've done it Clay's way and.....

I scheduled an special cron job kill the cron daemon. The system was idle so there was no need to have cron do anything for that holiday.

After the holiday I dialed right back in and restarted the deamon.

/sbin/init.d/cron start

Low tech, but for weekday holidays it worked fine.

My dba is complaining about Oracle backups kicked Passover, so I guess it's time to get Clay's script and implement it first test, then prod.

Good luck.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com