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.
?
“A potato doesn’t have a CPU, memory or storage space, so it was quite a challenge,” said Johan Piest of the Linux on Anything (LOA) group. “Obviously we couldn’t use a large distribution like Fedora or Ubuntu, so we went with Damn Small Linux.”