1834648 Members
2710 Online
110069 Solutions
New Discussion

Re: cron on 3rd friday

 
SOLVED
Go to solution
Vlad_11
Frequent Advisor

cron on 3rd friday

Hi,
Is there any way to schedule cron execution on
every 3rd friday each month ?

so far I'm using:
0 12 * * 5 /appl/email/suka

but it's for every friday and shell handles this internally checking for existince some control files.

Thanks all.
D
beer or not beer
11 REPLIES 11
Patrick Wallek
Honored Contributor

Re: cron on 3rd friday

There is no way with cron itself to tell it to run the 3rd Friday of each month.

The way you are doing it is probably the best way there is.
Pete Randall
Outstanding Contributor
Solution

Re: cron on 3rd friday

D,

Something like this might do it

0 12 15-21 * 5 /appl/email/suka

Either that or it will give everyone a chance to pick on me for being an idiot (again)!


Pete


Pete
Pete Randall
Outstanding Contributor

Re: cron on 3rd friday

D,

I'll call myself an idiot first - that would run the job every day from the 15th through the 21st and every friday.


Pete


Pete
John Poff
Honored Contributor

Re: cron on 3rd friday

Pete,

I was about to post the same answer but I bailed out after I tested it. It looks good but the 'monthday' and 'weekday' parameters are cumulative, so it runs every Friday. Bummer!

JP
Steven E. Protter
Exalted Contributor

Re: cron on 3rd friday

What you could do is this:

Run the program every friday.

Use A. Clay Stephenon's caljd.sh program to figure out whether its the third friday in the month and execute only if its the third friday.


outline

#program start

# What friday number is it ???

# If its not the third firday.
exit 0;
else
execute desired code.

SEP

To find caljd.sh, go to procura's web site.

http://www.cmve.net/~merijn/

Attaching my version which is not current.

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
A. Clay Stephenson
Acclaimed Contributor

Re: cron on 3rd friday

You can't run on the 15-21 as the month day AND weekday 5 because cron does not AND these conditions but rather it OR's them. What you can do is use your same cron entry to run every Fridate and then a simple test to see if the month day is between 15 and 21 -- the 3rd Friday.

#!/usr/bin/sh

typeset -i10 DAY=$(date '+%d')
if [[ ${DAY} -ge 15 && ${DAY} -le 21 ]]
then
echo "Your commands go here."
fi

If it ain't broke, I can fix that.
Pete Randall
Outstanding Contributor

Re: cron on 3rd friday

JP,

"Like minds think great" or something like that.

;^)


Pete


Pete
Donny Jekels
Respected Contributor

Re: cron on 3rd friday

it might seem like a lot of work, but now you have a very pwoerfull date to julian convertion tool (attached)

use it any sitp that you want to do date manipulation work. such as what you are trying to perform.

or if you want to run a job on the 3rd business day of each month through cron. wierd, not uncommon.

1st step. find out what is the day count for each 3rd friday of the year and record that list.

2nd step, copy this conf tool in you script and source it to do date convertion for you.

DT=`date +%D`
echo "$DT\nq\n" | date2julian | awk '{print $9}' | read DATE
echo "$DT" | sed "s/\// /gp" | read M D Y # today's date
(( YESTERDAY = $DATE - 1 ))
echo "$YESTERDAY\nq\n" | date2julian | awk '{print $7}' | read ETAD
echo "$ETAD" | sed "s/\// /gp" | read MM DD YY # yesterdays date

once you have this info, you can do the date check.

say your list is 5, 15, 23, 32, etc...

then you want to do a check if this is the 3rd friday of the month.

if [ $DATE = 5 || $DATE = 15 || $DATE = 23 .... ]
then
echo this is 3rd friday
do your job
else
# not 3rd friday
exit 0
fi

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

Re: cron on 3rd friday

uh-oh, forgot to tell you to add the job in cron to run everyday. this does'nt matter, because the script will determine if it is the correct day.

peace
Donny
"Vision, is the art of seeing the invisible"
A. Clay Stephenson
Acclaimed Contributor

Re: cron on 3rd friday

Because thisa has been such a frequent request, I've decided to add yet another option, "-N", to caljd.sh. Your cron job should run every Friday but add this test to determine if the current daye is the third Friday [ or any 3rd occurence of a given weekday].

WEEKNO=$(caljd.sh -N)
if [[ ${WEEKNO} -eq 3 ]]
then
echo "3rd Friday; your commands go here"
fi

To determine if a given date is the 2nd occurrence of the weekday in a month, you could do this test:

WEEKNO=$(caljd.sh -N 9 12 2003)
if [[ ${WEEKNO} -eq 2 ]]
then
echo "2nd"
fi

Invoke as caljd.sh -u for full usage. This is caljd.sh Version 2.21.

If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: cron on 3rd friday

Here is the Perl version. The arguments are exactly the same as caljd.sh simply substitute caljd.pl for caljd.sh, if you like.

Here is caljd.pl, Version 2.21p.

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