Operating System - HP-UX
1825927 Members
2941 Online
109689 Solutions
New Discussion

Make a cron run on 1st non-weekend day of the month

 
SOLVED
Go to solution
John Chaff
Advisor

Make a cron run on 1st non-weekend day of the month

Hello,

I have tried this cron entry
0 5 1 * 1-5 /usr/local/bin/notify
but it doesn't work right.

I want this script to run on the first non-weekend day of each month. My command runs every Monday through Friday. Is there a way to do this in unix?

TIA.
John
5 REPLIES 5
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Make a cron run on 1st non-weekend day of the month

This is a case of cron doing just what you tell it to. The monthday and weekday cron fields actually OR (as opposed to AND'ing) each other. You want an AND operation (kinda, sorta) because that ain't right either.

What you really need to do is drop the monthday requirement replacing your '1' with '*' and your command will the run every Mon thru Fri. We then have to put some smarts in the command itself to determine if it should run or simply exit.

Here's my cut at it:

#!/usr/bin/sh

if [[ $(caljd.sh -M) -ne $(caljd.sh -p 1 -x 0 -x 6 -M) ]]
then
echo "First working day of the month; do your thing."
else
echo "It aint; don't do nothing; exit."
fi

You'll need the attached script to complete your task and make sure that you set and export PATH to include caljd.sh's location.
Invoke as caljd.sh -u for usage and examples and to see why this works.

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

Re: Make a cron run on 1st non-weekend day of the month

I would take a slightly different approach from Clay:

I would do the cron entry like:

0 5 1-3 * * /usr/local/bin/notify

I would then modify the script so that it check if the day of week a Sat or Sun or not.

DOW=$(date +%a)

if [ ${DOW} = "Sat" -o ${DOW} = "Sun" ] ; then
exit 1
else
do your stuff here
fi
A. Clay Stephenson
Acclaimed Contributor

Re: Make a cron run on 1st non-weekend day of the month

Because this seemed like deja vu all over again, I'll reference a recent very similar question in which I expand upon your idea to include holidays.

http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=734757
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: Make a cron run on 1st non-weekend day of the month

I should also add that caljd.sh is also very handy for properly setting up your /etc/acct/holidays file(s) because it will easily calculate the day offset values for the file bu subtracting the Julian Day of Dec 31 of the prior year from a given day's Julian Day:

e.g. Christmas 2004

OFFSET=$(( $(caljd.sh 12 25 2004) - $(caljd.sh 12 31 2003) ))
echo "Offset = ${OFFSET}"

This suggests a simple script for these calculations but I'll leave that as a student exercise (or maybe I just don't know how).

No points for this, please.
If it ain't broke, I can fix that.
John Chaff
Advisor

Re: Make a cron run on 1st non-weekend day of the month

Thanks A. Clay and Patrick. I used A. Clay's method because I really wanted to exclude holidays as well but I "knew" that wasn't possible. I have set up my holidays file for 2004 and 2005 and now I'm ready to roll.

Thanks again guys for all your help.

Regards,
John