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:
Note: The day of a command's execution can be specified by two fields -- day of month, and day of week. If both fields are restricted (ie, are not *), the command will be run when either field matches the current time. For example, ``30 4 1,15 * 5'' would cause a command to be run at 4:30 am on the 1st and 15th of each month, plus every Friday.
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
or
[ $(( ($(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
More Articles by Anthony Lawrence - Find me on Google+ 2013-08-06
Have you tried Searching this site?
Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates
This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more. We appreciate comments and article submissions.
Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.
I am a Kerio reseller. Articles here related to Kerio products reflect my honest opinion, but I do have an obvious interest in selling those products also.
Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.
We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.
Click here to add your comments - no registration needed!
Don't miss responses! Subscribe to Comments by RSS or by Email
Click here to add your comments
If you want a picture to show with your comment, go get a Gravatar