Re: month end cronjob

 
himacs
Super Advisor

month end cronjob

Hi admins,

I want to schedule a cronjob which should run every month end(last day of every month) 10 PM.
But with every month last date is varies.How i can achieve the same.

Please suggest

Regards
himacs
4 REPLIES 4
Pete Randall
Outstanding Contributor

Re: month end cronjob

Have you heard of Google yet? Try google.com and fill in the following search string:

end of month cron +site:itrc.hp.com


Pete

Pete
Dennis Handly
Acclaimed Contributor

Re: month end cronjob

>I want to schedule a cronjob which should run every month end (last day of every month) 10 PM.

You could just make 3 crontab entries. And handle Feb 29 special every 4 years.
James R. Ferguson
Acclaimed Contributor

Re: month end cronjob

Hi:

You can let your script execute every day and test for the end-of-month this way:

# [ "$(date +%d)" = "$(cal|awk 'NF>0 {LAST=$NF};END{print LAST}')" ] && echo last_day || echo skip_it

As usual, TMTOWTDI

...which Google, as Pete suggested, will expose.

Regards!

...JRF...
Bill Hassell
Honored Contributor

Re: month end cronjob

A similar way is to run the script on the 28,29,30,31 of every month:

1 1 28.29.30,31 * * myscript

Then in your script, use cal to locate the last day of the month:

LASTDAY=$(echo $(cal) | awk '{print $NF}')
[[ $(date '+%e') -ne $LASTDAY ]] && exit

So when the script runs, it will exit immediately if today's day of the month is not the last day. This method always works since the cal command does all the work handling days in each month, leap years, etc.

The cal command prints a formatted month but when used with echo, it is a string of numbers with the last number always the last day.


Bill Hassell, sysadmin