Cron day of month and day of week requirements
Posted on 2006-05-29
I'm trying to schedule a report to automatically run three days before a meeting. The meeting participants want a few days to review the report before they meet. The meeting is on the third thursday of the month. So, I want to schedule a cron job to run 3 days before the third thursday of the month.
Looking at a calendar I see I want to send this on Mondays. But only on Mondays that fall between the 12th and the 18th inclusive. The following does not work.
30 9 12-18 * 1 /path/to/script
The above runs at 9:30 AM every Monday. It also runs at 9:30 AM on the 12,13,14,15,16,17,18 of the month. There seems to be an "OR" relationship between the "Day of Month" and "Day of Week" fields. If either field is true the job runs.
Is there a way to change the logic so that the script will only run if both fields are true? In other words, the default behavior for cron is an OR relationship between Day of Month and Day of Week. Is there a slick way to change this to an AND relationship? That way the script will only run on Mondays that fall between the 12th and 18th of the month inclusive.
My current workaround looks like this ... cron calls script on the 12th-18th
30 9 12-18 * * /path/to/script
Inside the script I check that it is Monday before executing anything.
if [ `date +%a`=Mon ]
... script goes here
This works, but I don't like having schedule information in both cron and in my script. It would be nice to keep all scheduling information in cron.