Operating System - HP-UX
1819807 Members
3196 Online
109607 Solutions
New Discussion юеВ

Cron job to run 1st Saturday of each month

 
SOLVED
Go to solution
nancy rippey
Trusted Contributor

Cron job to run 1st Saturday of each month

Is it possible to have a cron entry to run on the 1st Saturday of each month?
nrip
10 REPLIES 10
Steven E. Protter
Exalted Contributor

Re: Cron job to run 1st Saturday of each month

Shalom Nancy,

cron can't do it.

Date hammer can calculate the date in a cron script.

http://www.hpux.ws/merijn/caldj.sh
http://www.hpux.ws/merijn/caldj.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: Cron job to run 1st Saturday of each month

Not with just cron but you can have the job run every Saturday and then let it decide whether or not this is the 1st (or Nth) Saturday.

Here's one approach: Create a cron entry that will run at your chosen time every Saturday, then:

#!/usr/bin/sh

export PATH=${PATH}:/usr/local/bin
typeset -i STAT=0
if [[ $(caljd.sh -N) -eq 1 ]]
then
echo "It's the 1st Saturday; do your thing"
# execute your command and set ${STAT}
fi
exit ${STAT}

This assumes that you have installed the attached caljd.sh in /usr/local/bin. Invoke as caljd.sh -u for full usage and many examples.

If it ain't broke, I can fix that.
Rick Garland
Honored Contributor
Solution

Re: Cron job to run 1st Saturday of each month

The 1st Sat of each month will be a date of 1-7. If the day today is '6' (date +%U) && date is day of month is "01-07' (date +%d)

If both of these conditions are met then today is the 1st Sat of a month
Yogeeraj_1
Honored Contributor

Re: Cron job to run 1st Saturday of each month

hi nancy,
also consider using the following as template for your crontab:

#*******************************************************************************
# min|hour |day |month|day |script
# | |of mo| |of wk|
#----|-----|-----|-----|-----|--------------------------------------------------
#*******************************************************************************
#*******************************************************************************
# END OF TABLE day0->Sunday day6->Saturday
#*******************************************************************************


hope this helps!

kind regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Muthukumar_5
Honored Contributor

Re: Cron job to run 1st Saturday of each month

Use format in crontab as,

minute hour monthday month weekday command

to run on 1st saturday then,

1-7 * *
In command script>

if [[ "$(date +'%a')" != "Sat" ]]
then
exit 0
else

fi

--
Muthu
Easy to suggest when don't know about the problem!
Arturo Galbiati
Esteemed Contributor

Re: Cron job to run 1st Saturday of each month

Hi nancy,

# minute hour monthday month weekday (0=Sunday) command
# 0-59 0-23 1-31 1-12 0-6
* * 1-7 * 6
This will run your script on Saturday only during days from 1 to 7 of the month.
i.e.: first Saturday of teh month
(environment: HP-UX 11i)

HTH,
Art
nancy rippey
Trusted Contributor

Re: Cron job to run 1st Saturday of each month

Thank you all for the valuable information. I tested with many of the ways given and they seem to work. Thanks again
nrip
Arunvijai_4
Honored Contributor

Re: Cron job to run 1st Saturday of each month

Hi Nancy,

You can find these docs for more information,

http://docs.hp.com/en/B2355-60127/crontab.1.html
http://www.adminschoice.com/docs/crontab.htm

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
A. Clay Stephenson
Acclaimed Contributor

Re: Cron job to run 1st Saturday of each month

This suggested solution will certainly run the cron job on the 1st Saturday of the month; however it will also run the cron job on EVERY Saturday of the month and, in addition, will also execute on the 1st through 7th days of the month.

# minute hour monthday month weekday (0=Sunday) command
# 0-59 0-23 1-31 1-12 0-6
* * 1-7 * 6
Dates specified as both days of the month and days of the week are logical OR's not
logical AND's.


If it ain't broke, I can fix that.
nancy rippey
Trusted Contributor

Re: Cron job to run 1st Saturday of each month

This thread is now closed
nrip