When cron won’t run a job with the schedule you want.
Cron is the job scheduler in any Linux/Unix system. Most of the time, it is pretty good, but when you need to apply several variables to running a job, it doesn’t quite cut it. If you want a tutorial on Cron, go here. I will just be covering some ways of making cron a little more flexible to help you get the right schedule you need.
If you want to run a job at noon every day. That is pretty simple. If you want to run a job 5 minutes after noon, that is very simple. If you want it to happen on any day of the month, again that works. If you want it on every Wed, again simple.
So what do you do if you need to apply some logic? Let’s say you want a job to only run on the first Saturday of the month, but only after following the first Sunday of the month? So if the first was a Saturday, you would not want the job to run? You might think that setting the day of the month as [2-8] and the Day as a Saturday might work? Wrong, it will do an ‘OR’ operation, not an ‘AND’. Basically it will fire on the 2nd, 3rd, 4th, all the way to the 8th. It will also fire on every Saturday. That is clearly not what you want.
The solution is actually quite simple. You can use the if/then operator of the command line. This is how I accomplished it and it works quite well (and I thought I would share).
0 12 2,3,4,5,6,7,8 * * if [ `date +\%a` = "Sat" ]; then sh /u01/app/oracle/scripts/somescript.sh 1 > /u01/app/oracle/scripts//u01/app/oracle/scripts/logs/somescript.log 2>&1 ; fi
So in the above example, I run this in the 2-8 day frame. It executes every single one of those days at 12 o’clock. I then check to see if the day is a Saturday. If it is, then execute the encapsulated command, if not then don’t run it. So while the job technically still executes everyday, I added some extra logic to enable it to do what I want, which is to only execute the script if that day is a Saturday.
If anyone has any other tips or ideas then please share them.
Noons said,
December 16, 2009 @ 6:03 pm
Theoretically, the command cron executes is a full blown shell command line, with all its bells and whistles, including full shell programming constructs.
In practice, I prefer to do more complex logic in a shell script that gets started by cron as needed. The date filtering – and/or any other filtering – is done in the script code.
Makes for an easier to read crontab and lets me change the filtering without having to change anything in crontab – ever had the cron daemon crash after a change in crontab?
I had: it was a regular event on RH3…
Thomas Roach said,
December 16, 2009 @ 6:19 pm
Definitely good feedback. I generally have put it in the script, and if so, a good comment in cron can give that information. With my approach, it is still readable in the crontab which isn’t that big of a deal. I could have also did 2-8 instead of writing all the dates out, which would have also made it easier to read and left the line uncluttered.
The lesson of all this is that if you can’t get the schedule you want in cron, you can add extra logic in the script/shell to do that for you.
I haven’t had it crash on me. I have done crontab -r by mistake and wondered why my jobs weren’t running
Jonathan said,
December 17, 2009 @ 5:35 am
I agree with Noons – keep the crontab tidy and put the logic in the script. If other people are editing the crontab it would be a lot harder to spot if they have messes up the syntax of your command.
Blogroll Report 11/12/2009-18/12/2009 « Coskan’s Approach to Oracle said,
January 2, 2010 @ 9:43 am
[...] 15-How to do complex scheduling with crontab? Thomas Roach-When cron won’t run a job with the schedule you want. [...]