Run a cron job every 5 minutes between two times

18

5

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.

pythonic metaphor

Posted 2010-04-21T20:37:21.480

Reputation: 2 046

Answers

24

You'll have to do it piecewise:

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

Ignacio Vazquez-Abrams

Posted 2010-04-21T20:37:21.480

Reputation: 100 516

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.

a CVn

Posted 2010-04-21T20:37:21.480

Reputation: 26 553