- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Cron job scheduling last saturday of month..
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-16-2011 11:28 PM
тАО01-16-2011 11:28 PM
need to schedule a job (to execute script) every month of last saturday..
Pls advice
Regards
Ali
Solved! Go to Solution.
- Tags:
- crontab
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-17-2011 12:33 AM
тАО01-17-2011 12:33 AM
Re: Cron job scheduling last saturday of month..
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-17-2011 02:51 AM
тАО01-17-2011 02:51 AM
Re: Cron job scheduling last saturday of month..
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..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-17-2011 03:02 AM
тАО01-17-2011 03:02 AM
Re: Cron job scheduling last saturday of month..
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-17-2011 06:16 AM
тАО01-17-2011 06:16 AM
Re: Cron job scheduling last saturday of month..
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-18-2011 07:13 AM
тАО01-18-2011 07:13 AM
Re: Cron job scheduling last saturday of month..
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
#------------------------------------------------------------------------------
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-19-2011 12:33 AM
тАО01-19-2011 12:33 AM
Solution#!/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 (( )).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-19-2011 08:28 AM
тАО01-19-2011 08:28 AM
Re: Cron job scheduling last saturday of month..
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-19-2011 08:36 AM
тАО01-19-2011 08:36 AM
Re: Cron job scheduling last saturday of month..
15 23 * * 6 [ $(date +"\%m") -ne $(date -d 7days +"\%m") ] && /path/to/your/script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-20-2011 01:11 AM
тАО01-20-2011 01:11 AM
Re: Cron job scheduling last saturday of month..
Possibly on Linux but not HP-UX. No "date -d".