- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- how to write a script to run last sunday in a mont...
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
Forums
Discussions
Discussions
Discussions
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
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
09-30-2004 01:28 AM
09-30-2004 01:28 AM
how to write a script to run last sunday in a month
Need some help to schedule a script to run last sunday in a month.
Thanks in advance.
TDN
- Tags:
- date arithmetic
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2004 01:36 AM
09-30-2004 01:36 AM
Re: how to write a script to run last sunday in a month
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=707481
Rgds...Geoff
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2004 01:59 AM
09-30-2004 01:59 AM
Re: how to write a script to run last sunday in a month
Thanks Geoff.
I think i do the easy way.
In my script the `date +%d` must be less than 22.
I try this first.
Regards
TDN
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2004 02:00 AM
09-30-2004 02:00 AM
Re: how to write a script to run last sunday in a month
Thanks Geoff.
I think i do the easy way.
In my script the `date +%d` must be greater than 22.
I try this first.
Regards
TDN
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2004 02:05 AM
09-30-2004 02:05 AM
Re: how to write a script to run last sunday in a month
There are date calculators abound. You can even find one in the forums here. Do a search for datecalc
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2004 02:37 AM
09-30-2004 02:37 AM
Re: how to write a script to run last sunday in a month
But we have to include lines on executable script to check weather
1> The day is equal to 22 and month is feb and year divisble by 4 ( leaf year) and exit from that script execution.
2> Check month 31 days, leaf year and check weather it is less than 25 there.
3> Check month 31 days, leaf year and check weather it is less than 24 there.
Execution of there check will not take more than 2 secs there. So that,
execute the script on crontab on 22-31 and the executed script checks the day settings and do execution / exit without execution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2004 02:49 AM
09-30-2004 02:49 AM
Re: how to write a script to run last sunday in a month
# 1> feb month
if [[ $(date +'%m') -eq 02 && $(($(date +'%Y')%4))) -eq 0 ]]
then
if [[ $(date +'%e') -eq 22 ]]
# Don't do this
exit
fi
fi
# 2> 31 days month and augest (08) month
if [[ $(($(date +'%m)%2) -eq 1 || $(date +'%m) -eq 08 ]]
then
if [[ && $(($(date +'%Y')%4))) -eq 0 ]]
then
if [[ $(date +'%e') -le 24 ]]
# Don't do this
exit
fi
else
if [[ $(date +'%e') -le 23 ]]
# Don't do this
exit
fi
fi
fi
# 3> 30 days month
if [[ $(($(date +'%m)%2) -eq 0 && $(($(date +'%Y')%4))) -eq 0 ]]
then
if [[ $(date +'%e') -le 23 ]]
# Don't do this
exit
fi
else
if [[ $(date +'%e') -eq 22 ]]
# Don't do this
exit
fi
fi
fi
Keep the sequence, and do exit after logging message in log file if you want there.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2004 02:51 AM
09-30-2004 02:51 AM
Re: how to write a script to run last sunday in a month
Clay Stephenson's date hammer script is probably the best way to go. If you are interested in hacking another method, you could try this trick:
cal | tail -2 | head -1 | awk '{print $1}'
which will give you the date of the last Sunday of the current month.
JP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2004 02:56 AM
09-30-2004 02:56 AM
Re: how to write a script to run last sunday in a month
#***************************************************************
#
# Description : This script should be run in cron every sunday.
# It will execute the desired command on the last
# Sunday of the month
#*************************************************************
#*************************************************************
# GLOBAL VARIABLE DECLARATIONS
#*************************************************************
BASE=/usr/local/bin
MONTH=`date +%m`
YEAR=`date +%y`
DAY=`date +%d`
COMMAND=
#*************************************************************
# BEGIN FUNCTION SECTION
#*************************************************************
twenty_eight () {
if [ $YEAR = 04 ]
then
if [ DAY -gt 22 ]
then
$COMMAND
fi
else
if [ DAY -gt 21 ]
then
$COMMAND
fi
fi
}
thirty () {
if [ DAY -gt 23 ]
then
$COMMAND
fi
}
thirty_one () {
if [ DAY -gt 24 ]
then
$COMMAND
fi
}
case $MONTH in
01|03|05|07|08|10|12) thirty_one;;
04|06|09|11) thirty;;
02) twenty_eight;;
esac
Rgds...Geoff
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2004 03:42 AM
09-30-2004 03:42 AM
Re: how to write a script to run last sunday in a month
# Check today's date equal to last sunday of current month
if [[ $(date +'%e') -ne $(cal | grep -v "^$" | awk '{ print $1 }' | tail -1) ]]
then
# exit
exit 1
fi
John. You are really great. I heard as " Think simple to do great" and found here. I wrote a big script lines but you showed your best here. Thanks for your guidance.
- Tags:
- cal
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2004 03:54 AM
09-30-2004 03:54 AM
Re: how to write a script to run last sunday in a month
cal | cut -c1-2|grep -v "^$"|tail -1 will give the date of the last sunday.
Now you have the date, set the script.
Anil
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2004 03:57 AM
09-30-2004 03:57 AM
Re: how to write a script to run last sunday in a month
Thanks for the praise. I like to keep things simple. I'm too lazy to do all that hard thinking! :)
You can modify your code just slightly and get the last day of the month for any weekday. Just move the 'grep' after the 'awk', so that the blank lines are removed after the dates are printed. For example, to test for the last Friday of the month, this would work:
if [[ $(date +'%e') -ne $(cal | awk '{ print $6}' | grep -v "^$" | tail -1) ]]
Then you just adjust the variable in the awk print statement for the day of the week you want ($1 through $7 for Sunday through Saturday).
JP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2004 03:58 AM
09-30-2004 03:58 AM
Re: how to write a script to run last sunday in a month
Rgds...Geoff
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2004 08:23 AM
09-30-2004 08:23 AM
Re: how to write a script to run last sunday in a month
#!/usr/bin/sh
export PATH=${PATH}:/usr/local/bin
if [[ $(caljd.sh -M) -eq $(caljd.sh -n 7 -M) ]]
then
exit 0
else
echo "Last Sunday in month; do your thing"
fi
The idea is that that it looks at the current day's month and compare's it to that 7 days hence. If they differ, it's the last time that weekday occurs in the month.
By the way, caljd.sh -N will yield the occurence of a given weekday within the month.
Invoke as caljd.sh -u for full usage and examples.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2004 11:14 AM
09-30-2004 11:14 AM
Re: how to write a script to run last sunday in a month
I was stunned by that one liner but that's a great try. For ex.,
cal 08 04 |tail -2 |head -1 |awk '{print $1}'
is not giving me the last sunday ;-).
I believe the best approach is given by Clay. We use similar logic here where some of our maintenance windows are on 2nd Sunday!!. Add 7 to the current date and if the resulting date is of next month, then that's it. Run it every sunday.
#!/usr/bin/ksh
NOW=$(/usr/contrib/bin/perl -e "printf("%d\n",time())")
MON=$(date +%b)
(( NEXTMON_SECS = $NOW + ( 7 * 86400 ) ))
NEXTMON=$(echo "0d${NEXTMON_SECS}=Y" |adb |awk '{print $2}' )
if [[ "$MON" != "$NEXTMON" ]]
then
echo "OK!.. this is the last sunday."
#your_function
else
echo "More sundays to go"
exit 0
fi
-Sri
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-01-2004 12:43 AM
10-01-2004 12:43 AM
Re: how to write a script to run last sunday in a month
August 4
S M Tu W Th F S
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
# cal 08 04 | cut -c1-2|grep -v "^$"|tail -1
31
Looks like last Sunday to me...
Rgds...Geoff
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-01-2004 12:45 AM
10-01-2004 12:45 AM
Re: how to write a script to run last sunday in a month
# cal 08 2004 | cut -c1-2|grep -v "^$"|tail -1
29
Rgds...Geoff
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-01-2004 01:12 AM
10-01-2004 01:12 AM
Re: how to write a script to run last sunday in a month
Thank you all of you.
I think I go for the solution to put the script in cron running every sunday and check out if the date is the last sunday inside the script.
I found a example on bigadmin.com
Once again, thanks guys.
Regards
TDN