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.
Using modulos arithmetic
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..
Compare file dates
Your script can do something like this if you have GNU touch:
rm -f mysentinel
if [ mysentinel -nt notuntil ]
touch -d "now + 3 days" notuntil
# do the rest of the script
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
Take a nap
Fired off by inittab:
# sleep 3 days
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