Operating System - HP-UX
1819873 Members
2471 Online
109607 Solutions
New Discussion юеВ

Crontab on first Tuesday of every month

 
SOLVED
Go to solution
Tim O'Connell
Regular Advisor

Crontab on first Tuesday of every month

Hi,

I need to run a script on the first Tuesday of every month. Is there an easy way to do this via crontab. I have tried 00 12 1-7 * 2 ksh script but as the entries are cumulative it runs on all dates from 1 to 7. I have done it previously on Unixware by writing a script to run the cal command and retrieve the date of the first Tuesday from there. Is there a simple way with cron.

Thanks,

Tim
5 REPLIES 5
Geoff Wild
Honored Contributor
Solution

Re: Crontab on first Tuesday of every month

Not easy, but check this thread - might help:

http://forums1.itrc.hp.com/service/forums/bizsupport/questionanswer.do?threadId=708384

Rgds..Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Steven E. Protter
Exalted Contributor

Re: Crontab on first Tuesday of every month

You can run a script every tuesday and ..

Use one of these scripts to decide if its the first Tuesday of the month and run or exit.

http://www.hpux.ws/merijn/caljd-2.23.sh

http://www.hpux.ws/merijn/caljd-2.2.pl

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: Crontab on first Tuesday of every month

The crontab entry you make does an "OR" when you were expecting an "AND" operation. There is no way for cron to do this on it's own. The solution is to create a cron entry that runs every Tuesday and then the script itself decides whether or not to execute or simply exit.

The solution is quite simple:

Use the mentioned caljd.sh; Version 2.23 is the latest and install it on your box; e.g. /usr/local/bin.

#!/usr/bin/sh

export PATH=${PATH}:/usr/local/bin

if [[ $(caljd.sh -N) -eq 1 ]]
then
echo "1st Tuesday; do your thing"
else
exit 0
fi

Invoke as caljd.sh -u for full usage and many examples.
If it ain't broke, I can fix that.
Tim O'Connell
Regular Advisor

Re: Crontab on first Tuesday of every month

Thanks for the replies. Got some ideas from the links
I think I will run the script on every Tuesday & use the following to get the date of the first Tues. Many Thanks. Points awarded

cal | awk '{print $3}' | grep -v "^$" | grep -v "Tu" | head -1

Rgds,

Tim
Rodney Hills
Honored Contributor

Re: Crontab on first Tuesday of every month

Run your script every tuesday and at the start of the script do the following-

typeset -i x=`date "+%d"`
if [[ $x -gt 7 ]] ; then exit

HTH

-- Rod Hills
There be dragons...