Quote:
Originally Posted by waelaltaqi
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?