Operating System - HP-UX
1833056 Members
2328 Online
110049 Solutions
New Discussion

Re: Cron / Scheduling Problem

 
SOLVED
Go to solution
Greg White
Frequent Advisor

Cron / Scheduling Problem

Hello everyone:

I need to schedule a cron job to run every fourth Friday at 11:00 PM. I can make the the cron job run every Friday but when I try to use the week number in the date command, it doesn't always work when I go from one year to the next. Is there an easy answer for this?

Thanks in advance, Greg
I know how to do it in pascal.
7 REPLIES 7
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Cron / Scheduling Problem

Hi Greg:

I have a deal for you. What you are going to do is turn todays date into a Julian Day. (~ the number of days since 4713 BCE). Astronomers use this method to make orbital calculations easy. I'll attach my caljd.sh to do all the hard work. I assume you already have the cron entry to fire off the command every Friday at 2300. We now just need to determine if this is the correct Friday and execute some command or simply exit. Try this:

#!/usr/bin/sh

GO_WK=0
# GO_WK should be in the range 0-3 depending
# on which week you wish to start this
export PATH=${PATH}:/usr/local/bin
STAT=0
WK=$(( (($(caljd.sh) + 1) / 7) % 4))
if [ ${GO_WK} -eq ${WK} ]
then
echo "Execute Command" # should set STAT
fi
exit ${STAT}

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

You should install caljd.sh in /usr/local/bin (or some other convenient place but set the PATH accordingly).

Now here's what it is doing.
We get the current Julian Day and add 1 to it. (that simply makes the weeks start on Sunday).
Next we integer divide by 7 to get the number of the week since 4713BCE. We then do a mod 4 to get a number in the range of 0 to 3 - just what you need.

You can execute cajld.sh -u for full usage.

Enjoy, Clay


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

Re: Cron / Scheduling Problem

I gotta hand to you Mr. Clay but you are a HELL OF A SCRIPT WRITTER!!!!

Richard
Darrell Allen
Honored Contributor

Re: Cron / Scheduling Problem

Hi Greg,

Cron, in and of itself, isn't smart enough to do what you want. My approach would be to schedule a cron job at 23:00 every Friday. That job would run a script that verifies the day_of_month is 22 - 28. If so, run your job else edit.

That works because the 4th Friday (or any other day of the week) is always the 22nd - 28th.

Just saw Clay's solution which is much more elegant. I'm going to have to check it out to see how I can begin using it.

Anyway, you could use my method.

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
Greg White
Frequent Advisor

Re: Cron / Scheduling Problem

Wow! Richard, I have to agree with you. This is just perfect! Clay, even my "$GO_WK" was zero. How did you know?

Darrell yours was good too but Clay's was perfect. I've been looking at his caljd.sh script and I can't begin to understand how it works but it does.

Thanks again, Greg
I know how to do it in pascal.
William Fishburne
New Member

Re: Cron / Scheduling Problem

This problem can be resolved using cron itself as well, the line in your crontab file would be:

00 10 22-28 * 5

This would perform the desired function on the fourth friday of every month at 10:00.

A. Clay Stephenson
Acclaimed Contributor

Re: Cron / Scheduling Problem

Hi William:

You may be correct but I interpreted Greg's question to be every 4th Friday (i.e. 28 days) rather than the 4th Friday of a given month. This was hinted at by his attempt to use the week number in the date command but only Greg knows for sure.
If it ain't broke, I can fix that.
Greg White
Frequent Advisor

Re: Cron / Scheduling Problem

Hello William:

Thanks for the response but Clay interpreted my question correctly. I apologize for being unclear. In our case, we have bi-weekly payroll runs and some special processing is done after every other payroll run rather than the fourth Friday of each month.

Greg

I know how to do it in pascal.