Skip to main content

Command Palette

Search for a command to run...

Random Delay Cronjob Execution

Updated
1 min read
Random Delay Cronjob Execution
H

I am a developer from Malaysia. I work with PHP most of the time, recently I fell in love with Go. When I am not working, I will be ballroom dancing :-)

Sometime there are many jobs to be executed at the same time (e.g. 12 AM daily), and it posed heavy load on the host. If the jobs are not time-sensitive, random-time delay can be introduced to mitigate such issue.

0    0    *    *    *    sleep $((RANDOM % 3600)); /the/execution/script.sh

RANDOM is a bash built-in variable that generates a random integer. RANDOM % 3600 generates a random integer between 0 and 3600 (exclusive). 3600 seconds is equivalent to 1 hour, you can adjust this number to suit your need. The sleep command creates delay in the specified number of seconds.

More from this blog

Hong's Tech Blog

110 posts

The blog is older than you know. I prefer counting from the emergence of one integral anomaly to the emergence of the next, in which case this is the forth version.