Run a script every other day but not on weekends


2013/08/06

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
 

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..

Compare file dates

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
 

Using "at"

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
 

Take a nap

Fired off by inittab:

while :
do
...
stuff
...
# sleep 3 days
sleep 259200  
done
 

A sentinel

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.





(OLDER) <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> -> Run a command every other day but not on weekends



Increase ad revenue 50-250% with Ezoic


More Articles by

Find me on Google+

© Anthony Lawrence



Kerio Samepage


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.

Contact us





While we all ooh and ahh over the reports and graphs, Google is quietly building an incredible pile of extremely valuable information. (Tony Lawrence)





This post tagged: