LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Cron Job at the end of every Month? (https://www.linuxquestions.org/questions/linux-software-2/cron-job-at-the-end-of-every-month-619400/)

waelaltaqi 02-07-2008 01:45 PM

Cron Job at the end of every Month?
 
This is a known limitation to cron. I found this somewhere and i'm not sure if works:
Code:

58 23 * * * [ `date +%d` -eq `echo \`cal\` | awk '{print $NF}'` ] && myJob.sh
Would this work by the end of everymonth? if yes would somebody explain why?

BrianK 02-07-2008 02:16 PM

Quote:

Originally Posted by waelaltaqi (Post 3049334)
This is a known limitation to cron. I found this somewhere and i'm not sure if works:
Code:

58 23 * * * [ `date +%d` -eq `echo \`cal\` | awk '{print $NF}'` ] && myJob.sh
Would this work by the end of everymonth? if yes would somebody explain why?

yes, it would work.

here's how:
- this cron executes at 11:58pm every day.
- the "&&" means the part2 is dependent on part1 (part1 && part2), therefore if part1 is false or fails, part2 doesn't run.
- breaking down part1:
- - date +%d - gives the date, just the day part, i.e. today it returns '07' (try it - you can run it on a prompt)
- - cal - displays a calendar. "echo `cal`" displays the calender on one line
- - piping the output of echo `cal` through awk allows you to print only the last element of the calendar, which is the last day in the month. So, for this month, it prints out '29' (as this is a leap year).
- - -eq returns true if the left and right are equal, so returns true only if the output of "date +%d" (todays date) is the same as the last day of the calendar.

so, today, this script will break down like so:

[07 -eq 29] && myjob.sh

... 07 is not equal to 29, so the -eq test returns false.
... because the -eq test returns false, the right side of the && will not be executed, i.e. myjob.sh will not be run.

myjob will only be run, this month, on the 29th, then the script breaks down like so:

[29 -eq 29] && myjob.sh

Make sense?

waelaltaqi 02-07-2008 02:31 PM

makes absolute sense. I was mainly confused about the 'awk' part. I know nothing about awk the rest was not too bad. I did some reading about date and the %d option is listed right there in the man page. It looks like i need to start educating my self a little in awk.
Thank you very for breaking this down for me.

danmurithi 04-04-2012 07:14 AM

cron jobs
 
thanks for the useful info

rknichols 04-04-2012 10:12 AM

I would use a more direct approach that's a little easier to understand:
Code:

[ `date +%m` -ne `date -d tomorrow +%m` ]
Note that the % sign is special in a crontab and needs to be escaped, so the crontab line would be:
Code:

58 23 * * * [ `date +\%m` -ne `date -d tomorrow +\%m` ] && myJob.sh

TB0ne 04-04-2012 11:09 AM

Quote:

Originally Posted by rknichols (Post 4644624)
I would use a more direct approach that's a little easier to understand:
Code:

[ `date +%m` -ne `date -d tomorrow +%m` ]
Note that the % sign is special in a crontab and needs to be escaped, so the crontab line would be:
Code:

58 23 * * * [ `date +\%m` -ne `date -d tomorrow +\%m` ] && myJob.sh

I'd do something similar:
Code:

05 10 * * * [ `date -d tomorrow +%d` -eq '01' ] && /path/to/script
No matter what date is the end of the month, the next day is always "01". The time code will kick it off, but the script will only run if tomorrows date is "01". :)

rknichols 04-05-2012 11:00 AM

Quote:

Originally Posted by TB0ne (Post 4644665)
I'd do something similar:
Code:

05 10 * * * [ `date -d tomorrow +%d` -eq '01' ] && /path/to/script
No matter what date is the end of the month, the next day is always "01". The time code will kick it off, but the script will only run if tomorrows date is "01". :)

That's even better! But, you do need to escape that pesky % sign:
Code:

05 10 * * * [ `date -d tomorrow +\%d` -eq '01' ] && /path/to/script


All times are GMT -5. The time now is 05:44 AM.