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 |
Quote:
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? |
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. |
cron jobs
thanks for the useful info
|
I would use a more direct approach that's a little easier to understand:
Code:
[ `date +%m` -ne `date -d tomorrow +%m` ] Code:
58 23 * * * [ `date +\%m` -ne `date -d tomorrow +\%m` ] && myJob.sh |
Quote:
Code:
05 10 * * * [ `date -d tomorrow +%d` -eq '01' ] && /path/to/script |
Quote:
Code:
05 10 * * * [ `date -d tomorrow +\%d` -eq '01' ] && /path/to/script |
All times are GMT -5. The time now is 05:44 AM. |