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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Crontab end of month 2

Status
Not open for further replies.

rneve

MIS
Oct 11, 2002
51
EU
I have to schedule a job every last day of the month. I'm using crontab for several jobs, so I want to schedule this job also via cron to have one schedule program.

In short my question.
How can I determin if today is the last day of the month?

Thanks in advance.
 
Crontab doesn't deal with the concept of end-of-month.
I suggest 4 crontab entries

mm hh 31 1,3,5,7,8,10,12 * your command
mm hh 30 4,6,9,11 * your command
mm hh 28 2 * your command
mm hh 29 2 * your command

HTH Dickie Bird (:)-)))
 
I've always taken the lazy way out in this situation -- I run end-of-month jobs at one minute past midnight on the *first* day of the month. Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
You could do

55 23 28-31 * * command

then in the command do the checking to see if it really is the last day.

E.g

#!/bin/ksh

Datem=`date +%m`
Dated=`date +%d`

if [ $Datem = "01" -a $Dated = "31" ]
then
run command
elif [ Datem = "02" -a $Dated = "28" ]
then
run command

you get the idea.

Regards Mike

--
| Mike Nixon
| Unix Admin
| ----------------------------
 
Thanks for the input. But (there is always a but). It is nog possible to run the command on 1 o'clock on the 1st of the next month.

The other suggestion are "work-around", because I still have to think about which year it is for the month februari. The system administrator who will operate this system will "forget" to change the contab (or script) when we have a special year!

I'm looking for a command that tells me what day it is tomorrow. Is tomorrow the 1st of the next month then I know for sure that today is the last day of the month... I don't have to check/update the crontab...

Thanks...



 
Mike didn't mean 1:00 am, he meant 00:01, ie one minute past midnight. Will that not do?
 
Nope that won't do, because the job has to run and end on the last day. So say start at 18:00 en finish at approx 22:00, before the month ending...

 
date --date="+1 day" should show you if tommorow is the 1st
 
It's not that difficult to find other method to calc the last day, mine was just idea.

Take a look at cal

or

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

Solution 2 (more generic, using perl5)

#!/usr/bin/perl -w
#
# last-day-of-month - check if today is the last day of a month
#
# Input: none.
# Output: none.
# Exit status: 0 (true) if today is the last day in a month, otherwise 1.
# Algorithm: Get localtime and advance the day of month by one. Let mktime
# normalize the result and check whether day of month became 1.
# Requires: perl5.
#

use POSIX;

@the_time = localtime (time);
++$the_time[3]; # Element 4 is the day of month [1..31]
if ((localtime (POSIX::mktime (@the_time)))[3] == 1) {
exit 0;
}
exit 1;

or

perl: $logDate = time() - (3600 * 24);
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
localtime($logDate);
--
| Mike Nixon
| Unix Admin
| ----------------------------
 
Hi:

A lot of this date arithmetic was covered in this thread:

thread822-445663

Regards,

Ed
 
mrregan

The date --date="+1 day" only seems to work on linux
not AIX, TRU64 & Solaris, which is a shame because it would have been useful.

Mike --
| Mike Nixon
| Unix Admin
| ----------------------------
 
cal | xargs | awk '{ print $NF }'

will tell you the last day of the current month - you could work that into your script?

Chris
 
similar one:

cal|tail -2|sort -r|head -1|nawk '{print $NF}

...but not as good as CHoggarth's


ART
 
Code:
test `cal | xargs -n1 | tail -1` = `date +%d` && myScript.sh
:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top