Operating System - HP-UX
1748011 Members
3876 Online
108757 Solutions
New Discussion юеВ

Cron job scheduling last saturday of month..

 
SOLVED
Go to solution
HP-UX_Ali
Regular Advisor

Cron job scheduling last saturday of month..

Hello Experts

need to schedule a job (to execute script) every month of last saturday..

Pls advice

Regards
Ali

9 REPLIES 9
Dennis Handly
Acclaimed Contributor

Re: Cron job scheduling last saturday of month..

crontab(1) says that the weekday and monthday fields are cumulative or OR. You'll have to select one and add a check in the script for the other. Since months have varying number of days, you probably want that check in the script:
30 23 * * 6 script

This will fire off the script on every Saturday and 2330.

In the script:
month=$(date %m)
day=$(date %d)
set -A monthdays 00 31 28 31 30 31 30 31 31 30 31 30 31

On leap years you need to increment ${monthdays[2]}.

So:
if (( day <= monthdays[month] - 7 )); then
exit 0 # not last
fi
HP-UX_Ali
Regular Advisor

Re: Cron job scheduling last saturday of month..

hello Denis

Thanks for the reply,

Can you give in correct format.. like if i want to run a script i.e /home/ali/check.sh
on every last saturday of the month @ 23:00 hrs, then how will be the script look like..

Pls confirm ...

Reagrds
Ali..

Dennis Handly
Acclaimed Contributor

Re: Cron job scheduling last saturday of month..

>Can you give in correct format?

I mostly had it:
# This script is run every Saturday and needs to check internally for last in month
00 23 * * 6 /home/ali/check.sh

check.sh:
#!/usr/bin/sh
# Need to check for last Saturday of month
...
month=$(date %m)
day=$(date %d)
set -A monthdays 00 31 28 31 30 31 30 31 31 30 31 30 31

#On leap years you need to increment ${monthdays[2]}.
# Need to add code here ...

# If day of month is not in the last 7 days, then exit
if (( day <= monthdays[month] - 7 )); then
exit 0 # not last
fi
James R. Ferguson
Acclaimed Contributor

Re: Cron job scheduling last saturday of month..

Hi:

You can leverage the 'cal' command. For the last Saturday of the current month, you could do:

#!/usr/bin/sh
LASTSAT=$(cal|awk '{if (NF==7) {SAT=$7}};END{print SAT}')
if [ "$(date +%d)" -eq ${LASTSAT} ]; then
echo "Last Saturday"
else
echo "not last Saturday"
fi

...Embed this logic in your script and exit if it isn't the last Saturday of the month. Otherwise run whatever it is that you need to do. Your crontab will specify the overall script to run *every* Saturday.

Regards!

...JRF...

Steve Post
Trusted Contributor

Re: Cron job scheduling last saturday of month..

How about this. You need to have it run on a specific set of days during the year.
The days have no rhyme or reason. Something like, the last Saturday of the month, except in December it is the 2nd Tuesday, and in June, it is the 15th.
Why the typeset? We do not want 07 to be seen as octal.

#!/usr/bin/ksh
#------------------------------------------------------------------------------
# start of SCHEDULE TEST
# 1. Have a $x1_listofdates in this program that gives a schedule.
# 2. Run the cronjob every day
# 3. Have the job exit out unless the day matches the schedule.
#
# note: typeset -i tells unix the value is an integer, not octal binary.
#------------------------------------------------------------------------------
typeset -i x1_day_of_month=`date +"%d"`
x1_month_of_year=`date +"%b"`
typeset -i x1_year=`date +"%Y"`

x1_datestring=`printf "%02d%s%4d\n" $x1_day_of_month $x1_month_of_year $x1_year`

#---SCHEDULE---o---SCHEDULE---o---SCHEDULE---o---SCHEDULE---o
# these days are when the script should run.
# they are the 2nd workday of each month.
x1_listofdates="
02Dec2010
05Jan2011
02Feb2011
02Mar2011
#---SCHEDULE---o---SCHEDULE---o---SCHEDULE---o---SCHEDULE---o

x1_FLAGRUN=0
echo "Run fancyjob today?"
for x1_D in $x1_listofdates
do
echo " today is $x1_datestring look at $x1_D from the list. "
if [ "$x1_D" = "$x1_datestring" ] ; then
echo "I see this day $x1_D is on the list."
x1_FLAGRUN=1
fi
done

if [ $x1_FLAGRUN != 1 ] ; then
echo "do not run fancyjob today"
exit
else
echo "ok. Run fancyjob today."
fi
#------------------------------------------------------------------------------
# end of SCHEDULE TEST
#------------------------------------------------------------------------------
Dennis Handly
Acclaimed Contributor
Solution

Re: Cron job scheduling last saturday of month..

Here is a corrected version of my script:
#!/usr/bin/ksh
month=$(date +%m)
day=$(date +%d)
year=$(date +%Y)
set -A monthdays 00 31 28 31 30 31 30 31 31 30 31 30 31

# On leap years you need to increment ${monthdays[2]}.
if (( (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0)) )); then
#echo "Leap year!"
(( monthdays[2] += 1 ))
fi

# If day of month is not in the last 7 days, then exit
if (( day <= monthdays[month] - 7 )); then
echo "Not last Saturday in a month"
exit 0 # not last
fi
echo "Last Saturday in a month"

This has the advantage of not forking cal(1) and awk(1) as in JRF's script.

>Steve: Why the typeset? We do not want 07 to be seen as octal.

Only 08 and 09 have the stinkin' octal problem and only in the Posix shell. And the error only occurs in (( )).
Alzhy
Honored Contributor

Re: Cron job scheduling last saturday of month..

Ali,

Use The Grat A. Clay Stephenson's caljd.sh script.. it works like a charm sir. Very flexible too.

Just search for it here in the HP-UX forums.


Where is A Clay anyway? Anyone heard from him yet?

Hakuna Matata.
Alzhy
Honored Contributor

Re: Cron job scheduling last saturday of month..

Or this?

15 23 * * 6 [ $(date +"\%m") -ne $(date -d 7days +"\%m") ] && /path/to/your/script

Hakuna Matata.
Dennis Handly
Acclaimed Contributor

Re: Cron job scheduling last saturday of month..

>Alzhy: 15 23 * * 6 [ $(date +"\%m") -ne $(date -d 7days +"\%m") ] && /path/to/your/script

Possibly on Linux but not HP-UX. No "date -d".