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

how to schedule cron for last day of month

Status
Not open for further replies.

robwall7

IS-IT--Management
Sep 25, 2001
17
0
0
US
I have seen this before but can seem to find the format for scheduling a cron job to run only on the last day of the month.

Right now I'm brute forcing the job by:

0 0 28,29,30,31 * * /mydir/mycronjob.pl

not effecient, but gets the job done.

Any ideas?

robwall7
We look for things,...things to make us go.
 
But that will run it four days in a row during the months with 31 days. Another brute force way to do it and not have it run 4 days in a row is to do:
0 0 28 2 * /mydir/mycronjob.pl
0 0 29 2 * /mydir/mycronjob.pl
0 0 30 4,6,9,11 /mydir/mycronjob.pl
0 0 31 1,3,5,7,8,10,12 /mydir/mycronjob.pl

Someone else might know a more elegant way.
 
This cron entry reboots a server on the first Monday of each month. If you can find a way to add 1 to the current date, get the resulting day-of-the-month and check if it comes out as 01, then you're sure that the current day is the last day of the month.

0 1 * * 1 [ `date +\%d` -le 07 ] && /sbin/shutdown -i6 -g0 -y > /dev/null 2>&1

Cheers.
 
In cases like this, it's normal to schedule the script to run every day, and then let the script itself decide whether or not to continue.

Read this FAQ on manipulating dates. If you're using shell, you should be able to figure how to calculate tomorrows date. You look to be running a Perl script, so it may be even easier with Perl.

FAQ - Faq80-953

Greg.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top