If you have a need to execute some server-side code at regular intervals then cron is an ideal solution. If, rather than execute code at regular intervals, you want to execute code at random intervals then cron on its own won’t do.
Combined with some PHP, cron can be used to execute code or call a script at random intervals.
Inspired by Stu, this isn’t so much a tutorial as the underlying PHP isn’t complex and is already well described elsewhere. Instead, I’ll run through the algorithm used to deliver the desired functionality.
When considering the design of our script, we need to consider our requirements. Firstly, we still have a need for the script to run reliably and cron meets that requirement well. Secondly, we want to be able to specify a time range within which a random value is chosen to be the interval between code execution.
To make things clearer, let’s refer to our randomising script as the script and the code we wish to execute at random intervals as the action. Keeping them each in their own file will help to separate the logic.
The Cron Job
We want our cron job to execute our script at frequent, regular intervals. Whether or not the action is carried out is left for our script to decide. The cron is simply the mechanism that invokes the script and tells it to carry out its processing.
The cron job that executes our script should be scheduled according to the granularity of time intervals required. If we want our action to be carried out in intervals of between 1 and 48 hours then we might set our cron job to execute our script on an hourly basis.
Whatever frequency you choose for the cron job, don’t upset your web host by running it too frequently. Although our script will be resource light, your webhost may still have an objection.
The Script Algorithm
Each time our script is executed, we want it to carry out the following:
- Check if there’s a timestamp already stored.
- If not then generate a random, future timestamp and store the value somewhere.
- If a timestamp has been stored and that timestamp is in the future, do nothing.
- If a timestamp has been stored and that timestamp is in the past, carry out the action, generate and store a new future timestamp.
By breaking the solution down into these simple steps, you can see the script should continue to work without any further manual intervention.
The Script In Action
When the script is first executed, it will search for an existing timestamp value stored in the designated file. If the file doesn’t exist then a new future timestamp is generated and stored in the file.
Here’s a screenshot of the output displayed with debug mode switched on:

Then each subsequent execution of the script before the time threshold is reached will not cause the action to be carried out.


Finally, when the script is executed after time threshold has been reached, the action is carried out:

See the script in action. Keep refreshing the page to mimic what your cron job would do.

Interesting and just what i was looking for.