Operating System - HP-UX
1820498 Members
3405 Online
109625 Solutions
New Discussion юеВ

how to schedule a cron job to run on the third thursday of the month.

 
SOLVED
Go to solution
John Henrikson
Regular Advisor

how to schedule a cron job to run on the third thursday of the month.

Hello,
I'm hoping someone can help me write this properly. I have a job I need to run on the 3rd thursday of the month, every month. I've tried the following line in my crontab but it doesn't work properly:
1 1 15,16,17,18,19,20,21 * 5 /optim/bin/testcron.scr #

Thanks so much!!!
11 REPLIES 11
Chris Wilshaw
Honored Contributor

Re: how to schedule a cron job to run on the third thursday of the month.

For a start, the 5 should be a 4. day 0 is Sunday, 1 Monday etc.
Helen French
Honored Contributor

Re: how to schedule a cron job to run on the third thursday of the month.

First thing first - The last position (day of the week) should be 4 for thursday! 5 is for friday!
Life is a promise, fulfill it!
John Henrikson
Regular Advisor

Re: how to schedule a cron job to run on the third thursday of the month.

You're correct of course, but the problem is its running each day that I have stipulated.. thanks though
Helen French
Honored Contributor
Solution

Re: how to schedule a cron job to run on the third thursday of the month.

One option is to call another date check script from the cron tab:

1 1 ..... * 5 /optim/bin/cron_date_check #

Add these lines in the script:
if [ `date |awk '{print$1}'` = "Thu" ]
then
/optim/bin/testcron.scr
fi
Life is a promise, fulfill it!
Chris Wilshaw
Honored Contributor

Re: how to schedule a cron job to run on the third thursday of the month.

OK,

Change the 5 (or 4) to a *

Then it should just run on the specified dates.

You can then build logic into your script to test if date +%a is equal to Thu, and only continue if it is.
Rodney Hills
Honored Contributor

Re: how to schedule a cron job to run on the third thursday of the month.

To quote the man page for crontab,

Note that the specification of days can be made in two fields: monthday and weekday.
If both are specified in an entry, they are cumulative. For example,

0 0 1,15 * 1 command

runs command at midnight on the first and fifteenth of each month, as well as every Monday.

Which explains why is is always running on those days.

HTH

-- Rod Hills
There be dragons...
MANOJ SRIVASTAVA
Honored Contributor

Re: how to schedule a cron job to run on the third thursday of the month.

Hi John


I think


1 1 15,16,17,18,19,20,21 * 4 /optim/bin/testcron.scr , should work fine



Manoj Srivastava
A. Clay Stephenson
Acclaimed Contributor

Re: how to schedule a cron job to run on the third thursday of the month.

You have a fundamental problem in that you are expecting that if you specify a weekday (4 - Thursdays) and a range of monthdays that both conditions must be true but in fact only one condition has to be true. The fields are OR'ed rather than AND'ed.

The correct solution is to run your command every Thursday (weekday 4) and then inside the script do a 2nd test.

e.g.

MDAY=$(date '+%m')
if [[ $(MDAY} -ge 15 && ${MDAY} -le 21 ]]
then
echo "Run your commands; it's the 3rd Thursday"
else
echo "Exit; not the 3rd Thursday"
fi
If it ain't broke, I can fix that.
Dietmar Konermann
Honored Contributor

Re: how to schedule a cron job to run on the third thursday of the month.

Hi,

I fear that cron' syntax is not able to handle it (maybe I'm missing something).

But you could use a workaound... schedule the job for EACH Thursday and check if it's the 3rd one, e.g.:

1 1 * * 5 (( ($(date "+%e")-1) / 7 +1 == 3 )) && echo This is the job!

Only a rapid prototype... hope it works. :-)

Regards...
Dietmar.
"Logic is the beginning of wisdom; not the end." -- Spock (Star Trek VI: The Undiscovered Country)
Dietmar Konermann
Honored Contributor

Re: how to schedule a cron job to run on the third thursday of the month.

Oops. Thursday is 4.

1 1 * * 5 (( ($(date "+%e")-1) / 7 +1 == 3 )) && echo This is the job!

"Logic is the beginning of wisdom; not the end." -- Spock (Star Trek VI: The Undiscovered Country)
Dietmar Konermann
Honored Contributor

Re: how to schedule a cron job to run on the third thursday of the month.

Last try... need to go home now. :)

1 1 * * 4 (( ($(date "+%e")-1) / 7 +1 == 3 )) && echo This is the job!

"Logic is the beginning of wisdom; not the end." -- Spock (Star Trek VI: The Undiscovered Country)