Home > Tips, Uncategorized > Needed a random timed cron; Got it

Needed a random timed cron; Got it

August 18th, 2009 Leave a comment Go to comments

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.

Categories: Tips, Uncategorized Tags: ,
  1. September 5th, 2009 at 13:09 | #1

    Nice idea, It suits not just PHP file execution. So all I need to do is to add this script to my cronjob?

  1. October 18th, 2009 at 08:23 | #1

+ forty seven = forty nine