Needed a random timed cron; Got it

Sorry this is nothing related to php, though I wanted to run a cron job or scheduled job at a random time, searching the Internet gave me lots of ideas but I found the apt solution only yesterday.

Create a shell script


r=$(j=`dd if=/dev/urandom count=1 2> /dev/null | cksum | cut -f1 -d" "`; expr $j % 50)
echo '/usr/bin/php -q autolinker.php' | /usr/bin/at now + $r min

actually the first part create a random number, my primary os and obsession being ubuntu linux, this was not a tough task 😉 though all *nix this should be possible. The second line actually reschedules the job with the at command. The above shell script was scheduled with cron to run at 1 min past 6.

In my case I wanted the job to be scheduled between 6:01 and 6:51 am. Hence the random number generated is masked against 50 using the mod operator, for any one who needs it in a span of 1 hour could do the mod on 3600 and do the same.