how to write a script to run last sunday in a month (728 Views)
Reply
Advisor
TDN
Posts: 14
Registered: ‎07-16-2000
Message 1 of 18 (728 Views)

how to write a script to run last sunday in a month

Hi,

Need some help to schedule a script to run last sunday in a month.

Thanks in advance.


TDN
Honored Contributor
Geoff Wild
Posts: 7,170
Registered: ‎06-04-2001
Message 2 of 18 (728 Views)

Re: how to write a script to run last sunday in a month

Check out this thread:

http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=707481

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Advisor
TDN
Posts: 14
Registered: ‎07-16-2000
Message 3 of 18 (728 Views)

Re: how to write a script to run last sunday in a month

Hi,

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
Advisor
TDN
Posts: 14
Registered: ‎07-16-2000
Message 4 of 18 (728 Views)

Re: how to write a script to run last sunday in a month

Hi,

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
Honored Contributor
Rick Garland
Posts: 4,470
Registered: ‎06-25-1997
Message 5 of 18 (728 Views)

Re: how to write a script to run last sunday in a month

The last Sunday of a month will typically be between the 24th and the 30th. There will be a couple of exceptions in which the month has 31 days and the last Sunday of the month is the 31th.

There are date calculators abound. You can even find one in the forums here. Do a search for datecalc

Honored Contributor
Muthukumar_5
Posts: 4,030
Registered: ‎06-09-2004
Message 6 of 18 (728 Views)

Re: how to write a script to run last sunday in a month

I hope we can not define it with cron time informations on crontab file settings.

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.
Easy to suggest when don't know about the problem!
Honored Contributor
Muthukumar_5
Posts: 4,030
Registered: ‎06-09-2004
Message 7 of 18 (728 Views)

Re: how to write a script to run last sunday in a month

On the executable script add the following lines after header (#!) as,

# 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.

Easy to suggest when don't know about the problem!
Honored Contributor
John Poff
Posts: 2,448
Registered: ‎05-22-2001
Message 8 of 18 (728 Views)

Re: how to write a script to run last sunday in a month

Hi,

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
Honored Contributor
Geoff Wild
Posts: 7,170
Registered: ‎06-04-2001
Message 9 of 18 (728 Views)

Re: how to write a script to run last sunday in a month

#!/bin/sh
#***************************************************************
#
# 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
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Honored Contributor
Muthukumar_5
Posts: 4,030
Registered: ‎06-09-2004
Message 10 of 18 (728 Views)

Re: how to write a script to run last sunday in a month

We can simply do this as,

# 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.
Easy to suggest when don't know about the problem!
Honored Contributor
RAC_1
Posts: 5,920
Registered: ‎03-21-2002
Message 11 of 18 (728 Views)

Re: how to write a script to run last sunday in a month

Why go through this all???

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
There is no substitute to HARDWORK
Honored Contributor
John Poff
Posts: 2,448
Registered: ‎05-22-2001
Message 12 of 18 (728 Views)

Re: how to write a script to run last sunday in a month

Muthukumar,

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


Honored Contributor
Geoff Wild
Posts: 7,170
Registered: ‎06-04-2001
Message 13 of 18 (728 Views)

Re: how to write a script to run last sunday in a month

Anil - wow - using cal - that's cool - that's worth 11 points in my book :)

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Acclaimed Contributor
A. Clay Stephenson
Posts: 17,825
Registered: ‎07-16-1998
Message 14 of 18 (728 Views)

Re: how to write a script to run last sunday in a month

Here is one method. First create a cron entry to run your script every Sunday; theyt script itself will then decide whether or not to simply exit and do nothing or continue.

#!/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.

If it ain't broke, I can fix that.
Honored Contributor
Sridhar Bhaskarla
Posts: 6,350
Registered: ‎08-15-2001
Message 15 of 18 (728 Views)

Re: how to write a script to run last sunday in a month

Hi,

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
You may be disappointed if you fail, but you are doomed if you don't try
Honored Contributor
Geoff Wild
Posts: 7,170
Registered: ‎06-04-2001
Message 16 of 18 (728 Views)

Re: how to write a script to run last sunday in a month

# cal 08 04
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
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Honored Contributor
Geoff Wild
Posts: 7,170
Registered: ‎06-04-2001
Message 17 of 18 (728 Views)

Re: how to write a script to run last sunday in a month

Of course cal 08 04 is 1904... :)

# cal 08 2004 | cut -c1-2|grep -v "^$"|tail -1
29

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Advisor
TDN
Posts: 14
Registered: ‎07-16-2000
Message 18 of 18 (728 Views)

Re: how to write a script to run last sunday in a month

Hi,

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
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.