Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

cron

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?

like image 297
alebal Avatar asked Nov 16 '25 17:11

alebal


1 Answers

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.

like image 188
Nate Eldredge Avatar answered Nov 18 '25 20:11

Nate Eldredge



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!