views:

1158

answers:

5

I know that I can have something run every five minutes in cron with a line like:

 */5 * * * * /my/script

What if I don't want it running at 12:00, 12:05, 12:10, but rather at 12:01, 12:06, 12:11, etc? I guess I can do this:

 1,6,11,16,21,26,31,36,41,46,51,56 * * * * /my/script

...but that's ugly. Is there a more elegant way to do it?

+4  A: 

Nope.

That's it.

Will Hartung
+8  A: 

Use your first schedule:

*/5 * * * * /my/script

And add this to the start of your script:

sleep 60

(Yes, this is a joke)

Sean Bright
I actually use a variation of this to start a process 1 sec before midnight.
Jon Ericson
Ah, the sheer power of sleep -1 ;-)
JB
A: 

sean.bright's joke got me thinking... why not use...

* * * * * /my/script

...and within the script do this...

#!/bin/bash
export WHEN=`date '+%M'`
echo $WHEN
export DOIT=`echo "$WHEN % 5" | bc` 
echo $DOIT
if [ $DOIT != 0 ] ; then
    echo "ha ha ha"
fi
echo "done"

...a kludge... maybe, but as ugly as the crontab... I don't know.

dacracot
Because you are running a process every minute that you know will fail to do anything useful.Also, I think your if statement is broken. ;-)
Jon Ericson
Fixed the if statement.
dacracot
On the one hand that's better. On the other hand, it's more work being done to find out if you should *not* do something.
Jon Ericson
+18  A: 
1-56/5 * * * * /my/script

This should work on vixiecron, I'm not sure about other implementations.

David Zaslavsky
+1 for staying focused amongst the noise :)
Sean Bright
Same syntax is also accepted by fcron, which covers the two most prevalent crons. I do not believe that dcron supports this, but that's rarer.
ephemient
A: 

I'd create a new script "delaystart" that takes a sleeping period as first parameter and the script to run as the rest. I'd make the script check the crontab line for the line with the script and only start the script if the line is not commented out. That makes it reusable, and ps won't report the script as running when it really isn't.

#!/bin/bash
sleeptime=$1
sleep ${sleeptime}
shift
if ( ! crontab -l | grep -e '#.+delaystart '${sleeptime} $* ) then
  $*
fi
Ludvig A Norin