Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Running script via cron job on last Sunday of each month. 1

Status
Not open for further replies.

amkipnis

Programmer
Apr 15, 2003
21
US
Hello, I have a need to run a script on last Sunday of the month. What is the best approach for this task. Any help is appreciated. Thanks. Alex.
 
I think that you would need to run a script every sunday and determine how many days there are left in the current month. e.g using cal...
Code:
#!/usr/bin/ksh

set -A arr $(cal)
days_in_month=${arr[$((${#arr[*]}-1))]}
day_of_month=$(date +%d)
days_remaining=$((days_in_month-day_of_month))
if [[ $days_remaining -lt 7 ]]
then
   echo run the script
else
   echo not the last sunday
   exit
fi
 
Igor, thank you very much. I have tested this code and it worked fine.
 
amkipnis,

Something like this should work as well...

Code:
#!/usr/bin/ksh

TODAY=$(date +%d)
LASTSUN=$(cal | awk '{if (length>1) print $1}' | tail -1)

if [ $TODAY -eq $LASTSUN ]
then
continue
else
exit
fi

John
 
Thank you for a nice tip, John. This is very usefull solution.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top