1842055 Members
2522 Online
110186 Solutions
New Discussion

Make cron skip holidays?

 
SOLVED
Go to solution
Robert Fisher_1
Frequent Advisor

Make cron skip holidays?

Next Mon., Sept. 2nd, is Labor Day. We have a number of routine tasks that are run each weekday (Monday through Friday). Is there a utility in UNIX to tell me if the current date is a holiday? My holidays file is kept up to date but I can't find any utility that uses it. I could change my crontab to skip Monday but I would like to find something that would let me skip any holiday just by looking in the system holidays file.

Thanks in advance for any help,
Bob
7 REPLIES 7
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Make cron skip holidays?

Well Bob, you could do a date '%j' and parse the /etc/acct/holidays file yourself but why bother.

This is much simpler:

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

Pretty simple.

Here's the required script. Just make sure that caljd.sh is in your PATH.


If it ain't broke, I can fix that.
Patrick Wallek
Honored Contributor

Re: Make cron skip holidays?

caljd.sh (or caljd.pl) strikes again! :)
A. Clay Stephenson
Acclaimed Contributor

Re: Make cron skip holidays?

If you had rather have a Perl version, here it is. Caljd.pl. Invoke it as caljd.pl (or .sh) -u for full usahe and you will see what the -h argument is doing.
If it ain't broke, I can fix that.
Shannon Petry
Honored Contributor

Re: Make cron skip holidays?

If you wanted to do things a bit harder (Clay makes it simple but some of us are suckers for the long way) then use the /etc/acct/holidays file. As long as it is correctly updated, then you can parse out the first string of valid julian dates(day of year).

I.E.
TODAY=`date +%j`
for DAY in `cat /etc/acct/holidays|grep -v ^\*|grep -v ^2002`; do
if [ "${DAY}" = "${TODAY}" ] ; then
echo "Today is a holiday"
exit 0
else
echo "">>/dev/null
#fall through, run code
fi
done

Of course this requires that you have a valid file /etc/acct/holidays updated anually on each system where you will be running these kinds of scripts.

Regards,
Shannon
Microsoft. When do you want a virus today?
harry d brown jr
Honored Contributor

Re: Make cron skip holidays?

Shannon, I think Robert identified the real hard way: manually modify crontab not to run things on monday :-0 watch out for those typo's :-0

live free or die
harry
Live Free or Die
A. Clay Stephenson
Acclaimed Contributor

Re: Make cron skip holidays?

Hi again Bob:

Because both New Year's Eve and the next New Year's Day are often holidays, I suggest that you take advantage of a little 'improvement' that I made to the holidays file. Setup /etc/acct/holidays_2002,/etc/acct/holidays_2003, ... files. You then symbolically link holidays_YYYY to /etc/acct/holidays so that all the accounting utilities work as usual. Caljd.sh looks first for the _YYYY file so it will even cover you when you cross year boundaries. If caljd.sh does find the _YYYY holidays file; it reverts to the 'vanilla' filename so in all cases it works as expected.


If it ain't broke, I can fix that.
Robert Fisher_1
Frequent Advisor

Re: Make cron skip holidays?

Great. This was just what I needed. Shannon, I'm a unix guy and I'm lazy. I think I'll just use Clay's method especially after he made me think of New Years as well. It looks like he covered all the bases.

You guys are great.

Bob