16

Is there a way to specify a job that runs every 5 minutes between some start time and some end time on business days in a crontab?

Update I think it's relevant that my start and end times are not at round hours. So, specifying 9-5 in the hour column isn't enough. I want 9:30-17:30.

23

You'll have to do it piecewise:

30-59/5 9 * * * script.sh
*/5 10-16 * * * script.sh
0-30/5 17 * * * script.sh
1

If Ignacio Vazquez-Abrams's answer doesn't really work for you, for example because the script needs a large number of parameters or the invocation criteria are non-trivial (or not time-bound), then an alternative approach is to make a simple wrapper script, call the wrapper script at regular intervals, and have the wrapper script check the current time and invoke the main script.

For example:

#/bin/bash

# Check to see if we should run the script now.
HOUR=$(date +%H)
MINUTE=$(date +%M)
if test $HOUR -lt 9; then exit 0; fi
if test $HOUR -eq 9 -a $MINUTE -lt 30; then exit 0; fi
if test $HOUR -eq 17 -a $MINUTE -gt 30; then exit 0; fi
if test $HOUR -gt 17; then exit 0; fi

# All checks passed; we should run the script now.
exec script.sh ... long list of parameters ...

This allows for encoding execution criteria more complex than cron's syntax readily allows for, at the relatively small expense of invoking a shell and a separate script regularly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.