Running Cron jobs at random intervals

Posted on 18-12-2018 by Nadir Latif


To run a Cron Job at random intervals we need to prefix the Cron Job command with the sleep bash command. For example to run a Cron Job every 5 to 7 hours at random intervals, we can use the following:

0 */5 * * * sleep $[RANDOM\%120]m; path-to-script

In the above code, the '%' has to be escaped. The code: $[RANDOM\%120] generates a random number between 0 and 120. This is the number of minutes that script will pause

The script will run at the times: 05:00+(rand(0, 120)), 10:00+(rand(0, 120)), 15:00+(rand(0, 120)), 20:00+(rand(0, 120)) and 00:00+(rand(0, 120))