Operating System - HP-UX
1825780 Members
2122 Online
109687 Solutions
New Discussion

Re: cron and the end of month

 
SOLVED
Go to solution
Tim Nelson
Honored Contributor

cron and the end of month

Does anyone know of a way to tell cron to run a particular script at end of month regardless of what day the end of the month falls on?? IE, 28,29,30, or 31.

One way would be to put multiple entries in cron, but due to the nature of my script this doesn't help me.

If this is not possible please let me know as well. This would be a nice UNIX enhancement.-----------Thanks,
Kirk Reindl
Unix Admin.
Milw, WI
8 REPLIES 8
John Palmer
Honored Contributor

Re: cron and the end of month

Hi Tim,

Could it run at 00:00 on the first of the month?

If not then cron can't do it. However a solution is to get cron to run you job on the 28th, 29th, 30th and 31st and for your job to determine whether it should run or not.

The way I acheive that is to work out what tomorrow is with a simple C program and if it's the 1st then carry on, otherwise just exit the job.

If you search the forums for date related issues, I seem to remmber that sample C routines were posted some time ago. You could also do it with perl.

Regards,
John
Rick Garland
Honored Contributor

Re: cron and the end of month

This has worked well for me

DAY='TZ=MST-17MDT date + %d'
if [ "$DAY" = "1" ]
then
do your stuff
fi

Depending on your TZ, make the necessary mods. What this is doing is going forward 24 hrs and looking what the date is. If the date is "1" then today is the last day of the month. If Feb 28 and tomorrow is Mar 1, the 1 will be returned and the test is true.

cron by itself will not do this.
Rick Garland
Honored Contributor

Re: cron and the end of month

This has worked well for me

DAY='TZ=MST-17MDT date + %d'
if [ "$DAY" = "1" ]
then
do your stuff
fi

Depending on your TZ, make the necessary mods. What this is doing is going forward 24 hrs and looking what the date is. If the date is "1" then today is the last day of the month. If Feb 28 and tomorrow is Mar 1, the 1 will be returned and the test is true.

cron by itself will not do this.
Mark Mitchell
Trusted Contributor

Re: cron and the end of month

What I do is have cron run a program and the program know weather or not to run on that day. Have a cron for 28-31 and then the program will look at the calendar and decide to run or quit.
Rick Garland
Honored Contributor

Re: cron and the end of month

Minor modification here. Since the date +%d returns 2 digits for the day of the month, change the "1" to "01".

if [ "$DAY" = 01 ]
then
....

Also, drop the "" around the 01 in the test statement
Ed Ulfers
Frequent Advisor

Re: cron and the end of month

This may be to simple to consider, but how about putting the server to default in a time zone a few hours ahead (users setting their time zone in the profile). Then you can run the root cron from the system time. The month end can be started a few hours before the actual new month starts without knowing the actual month-end (only month start needed).
Put a smile on your users face, offer them a kiss (Hershey's Kiss).
Manju Kampli
Trusted Contributor

Re: cron and the end of month

There are many of of doing it, none of them are straight way.

We had similar situation, had no readymade option available with in Unix Cron. We will have to manipulate the date and time zone commands to come out of this. As Rick mentioned above setting the Timezone 24 hours ahead, so that you can check the next day date looks very impressive to me.

otherway is to schedule a cronjob for date 30 on month 4,6,9 and 11. Date 31 on month 1,3,4,7,8,10 and 12. and for date 28 on month 2.
During the leap year, you may have to change the date for month 2 entry.
Never stop "LEARNING"
Dave Walley
Frequent Advisor
Solution

Re: cron and the end of month

Hi.

I have read the reply from Rick and I have been trying to run this script on my system with no sucess yet. My TZ is GMT0BST I have tried the following, but DAY is not being populated. Can you tell me how I should amend my script.

Hope you can help.

Dave

DAY=`TZ=GMT+24BST date + %d`
echo $DAY
if [ "$DAY" = 06 ]

why do i do this to myself