[HPADM] Re: SUMMARY: cron on last day of month

From: Dan Chrastil (dchrastil@bcbsga.com)
Date: Fri Sep 27 2002 - 11:20:25 EDT


What a list, thanks to everyone for all the responses (way to many to list). However here is what
has poured in:

------------------------------------------
#!/bin/sh

day=$(TZ=MET-24 date +%d) # will look at the next day
if [[ $day -ne 1 ]]
then
exit 0 # not the last day of month
fi
# start here with your code
----------------------------------------
Here's a couple of ways. Both try to determine if tomorrow's date is "1"
indicating that today is the last day of the month...

Solution 1 - Using the date command...

00 01 * * * [ `TZ=$TZ-24 date +%d` = 1 ] &&
command-to-run-on-the-last-day-of-the-month

Solution 2 - Using Perl

Here's a small Perl script that will return an exit code of zero if
tomorrow's date is "1" or an exit code of one otherwise:

- - - - - snip - - - - -
#!/usr/bin/perl
# add 86,300 seconds (one day) to the current epoch time and extract
tomorrow's date
exit(0) if ( (localtime(time() + 86300))[3] == 1 );
exit(1);
- - - - - snip - - - - -

Assuming you call this script "last_day_of_month" and put it in
/usr/local/bin, you can use it in your crontab as follows:

00 01 * * * [ `TZ=$TZ-24 date +\%d` = 1 ] &&
command-to-run-on-the-last-day-of-the-month
------------------------------------------------------------
Here is a script I received from this group. I no longer have a need to run
this script on the last day of the month but I still had it laying around:

"month-end script"
==============================
if [ $(sh -c "TZ=$(date +%Z)-24; export TZ; date '+%d'") -eq 1 ]
then
        <some executable stuff here>
fi

Then in cron you set up this script to execute on the 28th, 29th, 30th and
31st of every month. Hopefully that makes some sort of sense.
------------------------------------------------------------------
This might be of interest:

    http://www.readout.fsnet.co.uk/projects/backdate/index.htm

The "fancy" bit is:

dim()
{
  m=$1
  y=$2

  cal $m $y | awk '{ print $NF }' | grep -v "^$" | tail -n 1
}

The dim (days in month) function gives the number of days in a given month
and year. So it would return 29 days for month number 2 (february) in 1996.

-------------------------------------------------------------
DO SOMETHING ON THE LAST DAY OF A MONTH

Unfortunately, cron does not give us a simple way to say
'run this on the last day of month'. Fiddling with the
different length months, leap years, y2k bugs and so on is
messy. Here's a simple solution that gives all the dirty
work to some well tested utility. The idea is to check whether
*tomorrow* is the first day of a month.

Solution 1 (using date, change MET to your local time zone)

#!/bin/sh
if test `TZ=MET-24 date +%d` = 1; then
   # today is the last day of month
fi

You can call this script from cron, say with a crontab of
4 2 28-31 * * /path/to/your/script
----------------------------------------------------

Dan Chrastil wrote:

> Can anyone shed some light on how to setup a cron to run on the last day of the month.
>
> Thanks


--
             ---> Please post QUESTIONS and SUMMARIES only!! <---
        To subscribe/unsubscribe to this list, contact majordomo@dutchworks.nl
       Name: hpux-admin@dutchworks.nl     Owner: owner-hpux-admin@dutchworks.nl
 
 Archives:  ftp.dutchworks.nl:/pub/digests/hpux-admin       (FTP, browse only)
            http://www.dutchworks.nl/htbin/hpsysadmin   (Web, browse & search)



This archive was generated by hypermail 2.1.7 : Sat Apr 12 2008 - 11:02:20 EDT