How to Run a Cron Job Randomly Every Hour in Ubuntu

cronlinuxUbuntu

I want a cronjob to run every one hour randomly. (i.e if the first job runs at 58 minutes,the second job should run at 47 minutes and the third one at 52 minutes and so on) But this should run randomly for everyone hour.

Best Answer

You can do this by defining a job which runs every hour on the hour, and sleeps for a random amount of time before running the command you're actually interested in. In your crontab:

SHELL=/bin/bash

0 * * * * sleep $((RANDOM*3600/32768)) && command

(You need to specify the shell, to ensure that $RANDOM is available. There are other ways of getting a random value for sleep if that's not appropriate.)

Related Question