Random Delay Cronjob Execution


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.
Subscribe to my newsletter
Read articles from Hong directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Hong
Hong
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 :-)