Operating System - HP-UX
1748222 Members
4499 Online
108759 Solutions
New Discussion юеВ

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

 
TDN
Advisor

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
17 REPLIES 17
Geoff Wild
Honored Contributor

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.
TDN
Advisor

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
TDN
Advisor

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
Rick Garland
Honored Contributor

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

Muthukumar_5
Honored Contributor

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!
Muthukumar_5
Honored Contributor

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!
John Poff
Honored Contributor

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
Geoff Wild
Honored Contributor

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.
Muthukumar_5
Honored Contributor

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!