Random Cron Job Help?

by 10 replies
13
Hi,

I'm trying to run a php script randomly x number of times daily.

I found a script online at www [dot] lampdocs [dot] com/blog/2008/09/running-a-cron-job-once-a-day-at-a-random-time that will run my script but randomly once per day only.

Anyone got any ideas they would like to share?

Thanks,
Jeremiah
#programming #cron #job #php #random #script
  • I'm not into that technical stuff, but here's something I found:

    moundalexis.com/archives/000076.php

    You might need more access to server-sided programs than your webhost allows...
    • [1] reply
    • Thanks for that, but I'm unable to view the site. I get...

      "The connection has timed out... The server at moundalexis.com is taking too long to respond."

      Maybe I'll try again later.
      • [1] reply
    • [2] replies
    • Thanks lisag, but that's where I started out.
      • [1] reply
    • Hi all,

      With mojojuju's kind help, THANKS! I come up with a script that seems to work for me.

      Don't laugh, I'm not a "programmer" but I think it gets the job done...

      <?php

      // Include the execution times
      include('execution_times.php');

      //get number of minutes passed today
      $this_minute = intval((date("H")*60) + date("i"));

      //At midnight, set-up run times
      if($this_minute == 0){

      //Set the number of times per day the script should run
      $executetimes = 10;

      //create an array of the random script times
      for($i=1;$i<=$executetimes;$i++){

      $random_integers .= intval(rand(1,1440)) . ", ";

      }

      //Create the file contents
      $file_contents = "<?php\n";
      $file_contents .= '$' . 'times_array = array(' . rtrim($random_integers, ', ') . ");\n";
      $file_contents .= '?>';

      //Save the file contents
      $file = fopen("execution_times.php", "w");
      fwrite($file, $file_contents);
      fclose($file);

      } else {

      //check for execution time match
      if (in_array($this_minute, $times_array)) {

      //execute whatever script you want here
      echo "running script";

      } else {

      //no match, do nothing
      echo "not now";

      }

      }

      ?>

      I created a cron job to trigger the above script every minute.

      Ta,
      Jeremiah
  • Banned
    [DELETED]

Next Topics on Trending Feed