alebal
alebal

Reputation: 5906

Crontab every 5 minutes, but not on 5,10,15, etc

Some of my sites need regular crontabs, I use this to start a cronjob every 5 minutes:

*/5 * * * *

The crontabs are small, light, but there are starting to be several sites that need them, and starting them all together, it starts not being a very good idea.

With this */5 the cron starts at 5, 10, 15 20, etc... is it possible to make it start at, for example 8,13,18,23, etc?

Upvotes: 2

Views: 5488

Answers (1)

Nate Eldredge
Nate Eldredge

Reputation: 58142

Vixie cron accepts steps in a range (thanks Keith Thompson), so you can do

3-58/5 * * * * my_command

With other versions of cron, this may not be supported and you'd just have to do

3,8,13,18,23,28,33,38,43,48,53,58 * * * * my_command

Another option is something like

*/5 * * * * sleep 3m ; my_command

This could be adapted to sleep for a random time, thus further spreading out the jobs. For instance, you could do

*/5 * * * * /bin/bash -c 'sleep $((RANDOM/(32767/180))) ; my_command'

or use SHELL = /bin/bash further up in your crontab to make the /bin/bash -c unnecessary, if you're okay with using bash to run all the other cron jobs following the SHELL = line. $RANDOM in bash expands to a random integer between 0 and 32767, so this gets you a delay of up to 180 seconds.

Upvotes: 4

Related Questions