SUMMARY UPDATE: cron schedule fire only on last day of month

From: Brehl, Blake <Blake.Brehl_at_anritsu.com>
Date: Thu, 13 Mar 2008 09:07:33 -0700

Received a very simple and clean script from Mark, using the timezone looking forward without hardcoding the timezone. Here it is:

________________________________________
>From Mark Deiss

D=$(TZ=`date +%Z`-24 date +%d)
if [ "$D" = "01" -o "${D}" = "1" ]; then
        echo today is the last day of the month
        echo execute something
fi

date +%Z pull out the current time zone on the server; you could hard code it to whatever is the fixed time zone value (i.e. TZ=EST-24 ).

date +%Z-24 advances the time zone one day - getting you the next day
date +%d displays the next day - some implementations may pad with a leading zero so the if statement that follows should handle either representation of the first of the next month.
______________________________________

Thanks Mark!

Best Regards, Blake
 

-----Original Message-----
From: Brehl, Blake
Sent: Wednesday, March 12, 2008 5:50 PM
To: Tru64 Mailing List (tru64-unix-managers_at_ornl.gov)
Subject: SUMMARY: cron schedule fire only on last day of month

Received many great replies. Ended up going with the 1st received, some really nice ksh scripts from the link Eric provided.

Thanks Everyone! Blake


_____________________________
>From Eric Sisson:
I suggest that you look at the following article:

   http://www.kingcomputerservices.com/unix_101/subtracting_dates.htm

It is a web posting of an article that was published originally in a print magazine called "SunWorld".

This article contains a script "monthdays" that calculates how many days are in each month; an adjacent script "yeardays" helps it account for leap years.

Using Unix command

   date +%Y%m%d

to get the current date as, for example,

   20080312

will allow you to enter command

   monthdays 20080312

to get the following result (continuing the example):

   31

Command

   date +%d

will tell you the current day of the month as, for example,

   12

If the result of "monthdays" and of the second "date" command are equal, then you are on the last day of the month.

I hope this helps.

Regards,
Eric Sisson

_________________________

>From Chris Wincentsen
Hi Blake,

I do this little "case" to check dates. You could modify this kind of logic for your needs...

#!/bin/ksh

cur_day=`date "+%d"`
cur_mon=`date "+%b"`
cur_mon_day=`date "+%b %d"`
date_var=""

# * Process last day of month...
case "$cur_mon"
in
    "Jan") date_var=" Jan 31" ;;
    "Feb")
           date_var=" Feb 29"
           check_29th=`last | grep -s " Feb 29"`
           if [ -z "$check_29th" ]
           then
             date_var=" Feb 28"
           fi
           ;;
    "Mar") date_var=" Mar 31" ;;
    "Apr") date_var=" Apr 30" ;;
    "May") date_var=" May 31" ;;
    "Jun") date_var=" Jun 30" ;;
    "Jul") date_var=" Jul 31" ;;
    "Aug") date_var=" Aug 31" ;;
    "Sep") date_var=" Sep 30" ;;
    "Oct") date_var=" Oct 31" ;;
    "Nov") date_var=" Nov 30" ;;
    "Dec") date_var=" Dec 31" ;;
        *)
# * Problem, set error value and exit...
           echo " Problem setting date variable."
           date_var="error"
           exit 1
           ;;
esac

if [ "$date_var" = "$cur_mon_day" ]
then
# * Process last day of month...
# submit job here
fi

----------------------------------------

The Feb 29th processing probably needs work for what you want. I actually look for the last day of the previous month, so I've changed my logic to be closer to what you might need.

Anyway, hope that helps!

Chris

______________________________________________
>From Orjan Pettersson
Don't know if it will compile on tru64 but http://sourceforge.net/projects/yest
is very helpful for date calculations in scripts
--
 Örjan
___________________________________________________
>From Franz Fischer
typeset perlscript='
my $now=time();
my _at_today=localtime($now);
my _at_tomorrow=localtime($now+86400);
exit($today[4]==$tomorrow[4]);
'
if perl -e "${perlscript}"; then
	echo "last day of the month"
fi
Regards
	\franz
_____________________________________________
>From Dr. Thomas Blinn
Fairly easy to find the month name from the date.
Fairly easy to build (by hand) a list of month names with the usual rule set, that is, 30 days hath Sep Apr Jun and Nov, all the rest are 31 day months except Feb for which you have to decide if it's a leap year (that is whether the year is divisible by 4, the next one for which that won't work isn't for another 200 years by which time you and I will be dead, so use a Feb rule of "the last day is 28 unless the year is a multiple of 4"), once you do that simple test (a case type logic may be easiest) you should have the decision tree for "is this the last day of the month".
I haven't written the script logic for you, but that's the basis for it.  Of course, if it's not the last day of the month, just exit the script, else keep going and do whatever needs to be done on the last day of the month.
If it's not time critical that the action happen some time like mid-day it is easier to do the task in the first minute of the first day of the month from the perspective of the first day of a new month always immediately follows the last day/hour/minute of the prior month.
Tom
_________________________________________
>From James Sainsbury
If you have gnu date installed its fairly simple
 	TOMORROW=`date +%d -d tomorrow`
 	if [ "$TOMORROW" -eq 1 ]; then
 		# today is the last day of this month
Failing that since you are in the US (where from our point of view it is always yesterday:) you can try setting the TZ variable to timezone ~ 24 hour ahead of yours.
eg
 	# if your timezone is GMT+7 then
 	TOMORROW=`TZ=GMT-17 date +%d`
You could work out the current offset from GMT for your locale and and 24 hours in the script.
-----Original Message-----
From: Brehl, Blake
Sent: Wednesday, March 12, 2008 11:16 AM
To: Tru64 Mailing List (tru64-unix-managers_at_ornl.gov)
Subject: cron schedule fire only on last day of month
Hi Admins,
 
I'd like to fire a job only on the last day of the month from crontab.  Using ksh and a crontab entry of 
 
1 3 28-31 * * /apps/scripts/lastdayfire
 
and have the script figure out which day is the last of the month.
 
Any ideas?  
Here's a sample script that runs only on the 1st 2 days of the month.
 
1 2 1-14 * * /apps/scripts/1st2mondaysrunit
 
#!/bin/ksh
# script name 1st2mondaysrunit
DAYOFWK=`date +"%Ou"`; export DAYOFWK
if [[ $DAYOFWK = 1 ]]
  then /apps/scripts/1st2mondaysrunit
fi
 
 
Best Regards,    Blake
 
Tru64UNIX 5.1A sp1
GS80
 
Blake Brehl
Sys Admin/DBA
Anritsu Company United States
Morgan Hill, CA
blake.brehl_at_anritsu.com
Received on Thu Mar 13 2008 - 16:09:06 NZDT

This archive was generated by hypermail 2.4.0 : Wed Nov 08 2023 - 11:53:45 NZDT