Back in the bad old days before Linux and Vixie cron, it required a bit of trickery if you wanted something to run once every other hour or every three days. For simple cases like that today, we'd just use a crontab line that might look like this:
0 */2 */2 * * echo "every other hour, every other day"
However, there are still conditions where you need something more restrictive. We can accomplish the "every other day but not on weekends" with crontab:
*/3 * * * 1,3,5 echo "`date` Not on weekends"
But things break down once we need to go much deeper. Perhaps more importantly, crontab can be confusing because there are two fields that control the days:
There are many other easy mistakes that can and have been made in crontab. While scripts can be just as prone to error, I think it's fair to say that modifying a working script might be easier than modifying a working crontab. With that in mind, let's see how we might approach staggered execution in other ways.
The idea is to run the script from crontab at some regular interval, but then exit if it's not the right time. For example, $(( ($(date +%s) / 86400) % 3 )) will evaluate to 0 every other day, so adding:
[ $(( ($(date +%s) / 86400) % 3 )) ] || exit 0
[ $(( ($(date +%s) / 86400) % 3 )) ] && exit 0
(depending on when you want the script to start running relative to today) will work. You'd put that at the top of your script.
Unfortunately, that breaks on leap years, so..
Your script can do something like this if you have GNU touch:
rm -f mysentinel touch mysentinel if [ mysentinel -nt notuntil ] then touch -d "now + 3 days" notuntil # do the rest of the script fi
Without GNU touch, you might do:
touch -t `date -v+3d +%Y%m%d%H%M.%S` notuntil
You run your script at startup (see Unix and Linux startup scripts) and add an "at" job that reschedules itself:
at now +3 days -f /path/yourscript
Presumably your script would do other tests to see if it should be running.
However, should anything go wrong, your script might not run "at". The usual way to fix that is to fire it off in cron regularly, test for the at job, and quit if it exists. That is redundant, but it's redundancy that keeps important things running.
If it doesn't particularly matter what time the script is rescheduled for, you might consider forcing the time with something like this:
at -t `date -v+3d +%Y%m%d0123`
That makes the job stand out in "at -l".
$ at -l 11 Fri Aug 9 01:23:00 2013
Fired off by inittab:
while : do ... stuff ... # sleep 3 days sleep 259200 done
In some situations, it may be easier to have some external script control the running of another. In that case, the irregular script may be fired off by cron or started with a respawn from inittab but it looks for the existence of some file or directory before it does its work.
Unlike typical file locking, you usually don't need to get ultra careful here. Presumably the action that creates the "stop" file will have done so in plenty of time before the checking process wakes up from its sleep or gets respawned by cron. It can be as simple as :
test -e stopfile || dosomething test -e greenlightfile && dosomething
Got something to add? Send me email.
More Articles by Anthony Lawrence © 2013-08-06 Anthony Lawrence
If you ask "Should we be in space?" you ask a nonsense question. We are in space. We will be in space. (Frank Herbert)