Random Cron Job Help?

10 replies
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
#cron #job #php #random #script
  • Profile picture of the author Dave Lianelli
    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...
    {{ DiscussionBoard.errors[1807462].message }}
    • Profile picture of the author Jeremiaho
      Originally Posted by Dave Lianelli View Post

      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...
      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.
      {{ DiscussionBoard.errors[1807607].message }}
      • Profile picture of the author mojojuju
        First things first, let's realize that we're talking about executing cron jobs at pseudo-random intervals. (sorry to be so picky, just felt like getting that out of the way because we're going to approximate things a little bit).

        I don't know what you're doing this for. One time I can think of needing something like this was when I was making lots of requests with the Twitter API, and I didn't want to set off any red flags, so I pseudo-randomized things a bit.

        Randomizing script execution throughout the day using cron jobs can be done pretty easily, and I can think of a couple of ways off the top of my head, but here's one of them.

        For purposes here, let's assume that you want to run a script called MyCoolPHPScript.php, randomly, X times a day.

        Well, you could set up a cron job, at 1 minute intervals, that executes the following script called Randomizer.php at 1 minute intervals. This Randomizer.php script would determine when your MyCoolPHPScript.php script is executed.

        Make the Randomizer.php script like this:

        <?php

        /* How many times a day do you want to run your script? */
        $executetimes = 5;

        // Pick a random number (there are 1440 minutes in a day)
        $random_integer = rand(1,1440);

        // Only when the 'randomness' threshold is reached, the script you want to run is executed. You could also add a random sleep interval before the inclusion of the script to make it seem more random.
        if ($random_integer <= $executetimes) {
        include('MyCoolPHPScript.php');
        }

        ?>

        So basically, a cron job runs the 'Randomizer.php' script every minute, but on average, your main script, MyCoolPHPScript.php, will only be run X times a day, where X is equal to the value of $executetimes.

        And if you need more exacting control over how many times the script should be run, that is, if you want to limit it to be run no more than 5 times a day, then you might want to have a counter written to a log file that can be checked upon to see if the limit has been reached.

        Ok, this irks me, you wrap the forum's 'PHP' tags around some PHP code and it parses out stuff, bah!
        Signature

        :)

        {{ DiscussionBoard.errors[1808034].message }}
        • Profile picture of the author Jeremiaho
          He mojojuju, many thanks for that!!

          However, if I'm following things right...

          Isn't possible that the statement ($random_integer <= $executetimes) may never be true and therefore the include file will not execute at all?

          Or it might only be true once or twice per day therefore I wouldn't be guaranteed in getting my daily X number of executions?

          Off-course, I could set a very high X value and cap the runs via a log file.

          Ta,
          Jeremiah
          {{ DiscussionBoard.errors[1808114].message }}
          • Profile picture of the author mojojuju
            Originally Posted by Jeremiaho View Post

            He mojojuju, many thanks for that!!

            However, if I'm following things right...

            Isn't possible that the statement ( <= ) may never be true and therefore the include file will not execute at all?
            That is correct.

            Originally Posted by Jeremiaho View Post

            Or it might only be true once or twice per day therefore I wouldn't be guaranteed in getting my daily X number of executions?
            That is correct as well.

            Upload the following script which runs through this procedure 1440 times, which should illustrate the variance of how many times your script should run in a day. Refresh it lots of times to see how the number of times the include script execution would change day by day.

            <?php
            /*
            $executetimes is the number of times you want your
            procedure to execute per day
            */

            $executetimes = 5;

            /*
            Choose a number between 1 and 1440 as there are 1440 minutes
            in a day
            */
            while ($i < 1440) {
            $random_integer = rand(1,1440);

            /*
            If the random number is less than or equal to $executetimes, then
            this script will do whatever it is you want it to do
            */

            if ($random_integer <= $executetimes) {
            echo 'Yep, the random number was '.$random_integer.', so we\'ll do something<br />';

            }
            $i++;
            }
            ?>

            Now seeing as it is important that your script runs no more than X times a day, you'll probably want to use a log file to keep track of things. You may also, at minute 1440 or 0 of the day just create X number of random numbers between 1 and 1440 which the cron'd script can read and only execute the stuff you want when the cron execution time matches one of your pre-chosen minutes.

            For instance, once a day, at the start or end of the day, when the cron script is runs, it could write X random numbers between 1 & 1440 to a "instruction file". If "X" were equal to 10, then the file could read as follows:

            524
            552
            987
            1064
            357
            1094
            350
            1189
            843
            1067

            So all of that might be stored in a file called execution_times.txt which would be regenerated at midnight every day. Then, every minute that the cron job is run for the next 24 hour period, and using any method you choose, the script can check the number of the minute of the day, corresponding to a number between 1 & 1440, with the numbers in this file. And if the minute of the day matches anything in this execution_times.txt file, then whatever script you wish to run can be run, if not, then it exits.

            So let's say it's 5:50 AM. Well 5:50 AM corresponds to the 350th minute of the day, which matches the number '350' in the execution_times.txt file. And in that case, your program would run.

            But at 5:51 AM, which is the 351st minute of the day, the script wouldn't run because 351 isn't in the randomly generated list of minutes. And your program won't be triggered to run again until 5:57 AM which is the 357th minute of the day and which also appears in execution_times.txt.

            At midnight, the cron script can be triggered to delete everything in execution_times.txt and create X more random minute values. Rinse and repeat the next day at midnight.
            Signature

            :)

            {{ DiscussionBoard.errors[1808200].message }}
  • {{ DiscussionBoard.errors[1809092].message }}
    • Profile picture of the author Jeremiaho
      Thanks lisag, but that's where I started out.
      {{ DiscussionBoard.errors[1809221].message }}
      • Profile picture of the author lisag
        Oops. I knew that :-)
        Signature

        -- Lisa G

        {{ DiscussionBoard.errors[1809279].message }}
        • Profile picture of the author mojojuju
          It's good to see you created something that works. I don't have a good PHP interpreter in my head, but what you posted seems like it would do the job.
          Signature

          :)

          {{ DiscussionBoard.errors[1812099].message }}
    • Profile picture of the author Jeremiaho
      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
      {{ DiscussionBoard.errors[1809258].message }}

Trending Topics