Operating System - HP-UX
1748257 Members
3970 Online
108760 Solutions
New Discussion

Re: HP-UX Cron - run every first thursday of the month

 
SOLVED
Go to solution
siulogoid
Occasional Contributor

HP-UX Cron - run every first thursday of the month

Hello,

 

I'm trying to cron something to run every first thursday on the month, but is not working properly.

 

I have:

 

0 10 1-7 * 4 /home/myscript > /dev/null 2>&1

 

so it should run at 10am, between day 1 and 7 of everymonth if weekday is 4 (thursday).

 

Instead it is runing everyday if 1-7....

 

Why not respecting the weekday?

3 REPLIES 3
Steven Schweda
Honored Contributor

Re: HP-UX Cron - run every first thursday of the month

> Why not respecting the weekday?

   If cron does day-of-month OR day-of-week (man cron), then it won't do
day-of-month AND day-of-week.

   The usual solution for problems like this is to get cron to do the
best it can, and then add tests in the script to skip the unwanted
cases.  For example, tell cron to run the script _every_ Thursday, and
have the script look for the _first_ Thursday.  (Use "date", and look
for a day-of-month from 1-7.)

   A Forum (or Web) search for keywords like, say:
      cron day
should find many examples.

siulogoid
Occasional Contributor

Re: HP-UX Cron - run every first thursday of the month

Added in the top of the script:

 

chk_day=`date +%a`
if [ $chk_day = 'Thu' ];
then

 

my code

 

 

else

exit 0;

fi

 

 

 

Kept in the cron every 1-7 day in each month.

and it seems it is working.

Dennis Handly
Acclaimed Contributor
Solution

Re: HP-UX cron - run every first thursday of the month

>Kept in the cron every 1-7 day in each month.

 

Yes, that makes the script easier.

 

>chk_day=`date +%a`

 

If you're worried about locales, you may want a day of the week in numeric format:

chk_day=$(date +%u)  # 1 - 7 Mon .. Sun

if [ $chk_day != 4 ]; then # Thu

   exit # if not Thursday, exit

fi

 

I hope you have comments in both crontab and the script to indicate how they work together.