cancel
Showing results for 
Search instead for 
Did you mean: 

Monthly cronjobs

SOLVED
Go to solution
arkie
Super Advisor

Monthly cronjobs

Hi,

Field descriptions of a crontab file say, 3rd field (Range 1-31). But, if I have to schedule a job to run every month end for a year starting Jan, how do i do that.

Pls suggest
8 REPLIES
James R. Ferguson
Acclaimed Contributor
Solution

Re: Monthly cronjobs

Hi:

The definition of "month end" might differ from the actual last day of a month. If is doesn't and its the actual last day that you want to run on, let your script determine if it should exit or continue.

One way for your script to decide if its execution is on the last day of any month is something like this, exiting if it isn't time:

...
MONEND=$(cal $(date "+%m %Y")|awk '/[1-3]/ {DAY=$NF};END{print DAY}')
[ $(date "+%d") -ne ${MONEND} ] && exit
...

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: Monthly cronjobs

Hi (again):

While it should be obvious from my initial remarks, given that it's the last day of any month that your script should run, I'd 'cron' it for the appropriate time something like this:

0 1 28,29,30,31 * * /path_toscript

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: Monthly cronjobs

Other than JRF's solution, could you convince management that 12:00 am on the first of the month is just as good?
arkie
Super Advisor

Re: Monthly cronjobs

Hi JRF,

Thanks for these great ways of doing things.

Hi Dennis,

We tried to convince the mgmt, our job could be easier, but they'r strict about having the getting the reports on the last day of every month.

Thanks!
mvpel
Trusted Contributor

Re: Monthly cronjobs

What if you ran the report for the previous month in the first minute of the first day of the following month? You can't get much more "month end" than 11:59:59.9999 (plus 1 second) on the last day of each month.

0 0 1 * * script
Viktor Balogh
Honored Contributor

Re: Monthly cronjobs

here is a much simpler method, you can get the last day of the current month like this:

# echo `cal` | awk '{print $NF}'

this is today:

# date +'%d'

to evaluate if today is the last day of the month:

# [[ $(date +'%d') -eq $(echo `cal` | awk '{print $NF}') ]]

you can concatenate this to your cronjob and run it every day:

00 23 * * * [[ $(date +'%d') -eq $(echo `cal` | awk '{print $NF}') ]] && /root/GEN_REPORT.sh

****
Unix operates with beer.
Viktor Balogh
Honored Contributor

Re: Monthly cronjobs

I found this also, it's a pretty simple method:

# [[ $(cal) = *$(date +%d) ]]

http://forums11.itrc.hp.com/service/forums/questionanswer.do?threadId=1158441
****
Unix operates with beer.
Dennis Handly
Acclaimed Contributor

Re: Monthly cronjobs

>mvpel: What if you ran the report for the previous month in the first minute of the first day of the following month?

Didn't I already suggest that? :-)