ITEM: AK2915L
How to run cron on the last day of every month.
Here are two ways to accomplish this.
The first is to add crontab entries similar to the following:
0 0 30 4,6,9,11 * command
0 0 31 1,3,5,7,8,10,12 * command
0 0 28 2 * command
This will work unless it is leap year, in which case
the crontab would have to be updated for the February entry.
It is a somewhat messy approach, but it will work.
An alternative would be to have a script that would run
at the beginning of each month. The script would create
a new crontab entry to run at the end of the current month.
It also would clear out the entry for the previous month.
The following script will do these two things:
NOTE: The following code is sample only and is provided AS-IS.
The incorporation of the following code into a user's
system and maintenance of this code is the responsibility
of the user. IBM does not provide technical support nor
provide maintenance for the following code.
\#!/bin/ksh
\#This script will create an entry in a users crontab that will
\#run on the last day of the current month. The script looks
\#for a cron entry from the month before, so you will need
\#to initially place an entry in the users crontab file. At
\#the beginning of the next month, run the script. It will
\#replace the entry from the previous month. If you had an existing
\#crontab entry that looked like this:
\#
\# 0 0 30 6 * command
\#
\# it would be replaced by the following:
\#
\# 0 0 31 7 * command
\#
\#This script will maintain a SINGLE crontab entry that will
\#always run at the end of the month.
\#Get the current/previous month and the current year.
MONTH=`date +%m`
YEAR=`date +%Y`
((MONTH=MONTH-0))
\#If the current month is January, then determine the previous year.
\#Otherwise, calculate only the previous month.
if [ $MONTH -eq 1 ]
then
((YEAR=YEAR-1))
((PREVMONTH=12))
else
((PREVMONTH=MONTH-1))
fi
\#Get the last day for the current month
LASTWEEK=`cal $MONTH $YEAR | sed "/\^$/d" | tail -1`
LASTDAY=${LASTWEEK\#\#*[1-3][0-9] }
\#Get the lastday for the previous month
PREVLASTWEEK=`cal $PREVMONTH $YEAR | sed "/\^$/d" | tail -1`
PREVLASTDAY=${PREVLASTWEEK\#\#*[1-3][0-9] }
\#Replace the previous month's crontab entry with the entry for
\#the current month.
\#
sed "s/$PREVLASTDAY $PREVMONTH \\* date/$LASTDAY $MONTH \\* date/" \\
/var/spool/cron/crontabs/root > /tmp/crontab.root.tmp
mv /tmp/crontab.root.tmp /var/spool/cron/crontabs/root
Support Line: How to run cron on the last day of every month. ITEM: AK2915L
Dated: July 1995 Category: N/A
This HTML file was generated 99/06/24~13:30:26
Comments or suggestions?
Contact us