Archive

Posts Tagged ‘crontab bluehost’

Running a Cron Job Once a Day at a Random Time

September 15th, 2008 3 comments

Let’s say we need to randomize the time to run a cronjob. We need to run a script once a day but the time should be different each day. How should we proceed?

I’ve got some solutions for this. The first one is a little stupid, but it will work if you have your own dedicated server. If you’re at a hosting service, this script will probably reach the timeout and will exit. As my third language after English and Russian is PHP, I will post some PHP solutiions here.

In all these samples you will need to run cron.php by your crontab. The line will look like:

*/n * * * * /usr/local/bin/php /home/user/domain.com/cron.php >/dev/null 2>&1

The stupid solution: cron.php

<?
set_time_limit(0);
sleep(rand(0, 86400));
/*
run everything you need here
*/

?>

The script above will run what you need after a random delay. Most probably this script will be killed by your hoster as it is a sleeping process and the first string of this script will not help you to keep it alive.

The second solution is more intelligent one. I don’t write anything like this at this time, I’d use
parse_ini_file, but this works and I didn’t like to make any changes to this script. Here we will have to create 2 files. The first one will be executed by your crontab, and the second will be used as a configurations file and should have 0777 in order to make it writable.

Let’s start with cron.php.

<?php

// Including the configuration file
include(‘cron_data.php’);

// Checking whether the main script was already executed
if( ($date == date(‘jm’)) && $done )
exit(‘The task is already completed’);

// Getting the parameters of current date
$ch = date(‘G’);
$cm = int(date(‘i’));

// Creating some random values for cronjob start at the first daily run
if( ($ch >= $mh) && ($cm >= $mm) )
{
$data = “<?php\n”;
$data .= ‘$date = ‘ . date(‘jm’) . “;\n”;
$data .= ‘$done = true;’ . “\n”;
$data .= ‘$mh = ‘ . rand(0, 23) . “;\n”;
$data .= ‘$mm = ‘ . rand(0, 59) . “;\n”;
$data .= ‘?>’;

// Putting the output to a file
$file = fopen(“cron_data.php”, “w”);
fwrite($file, $data);
fclose($file);

/*
do anything you need here
*/
}

?>

The second file is used to store settings and is named cron_data.php.
<?php
$date = ”;
$done = false;
$mh = 0;
$mm = 0;
?>

Let’s analyze what will be done to ensure random start time. We are running cron.php every n minutes (depends on how randomly you need to run this script). On the very first run the script will run the command you need after the first run and it will create some startup data for the next day. On the next day the time of script execution will be truly random: it will be defined by rand(0, 23) and rand(0, 59).

These solutions can be used when you have the ability to setup cron jobs, no need to have access to your crontab file. Let me show you how to set this up on a CPanel hosting (like bluehost.com, hostmonster.com, etc).

After logging into your account, you should go to Cron Jobs (that’s usually located at the bottom).

Then I’d suggest you to select basic or standard cron interface.

Then select how often you are going to run your command. Then, if you want to run PHP from command line, you should ask your support, where the PHP binary is located. In a hostmonster.com sample the location was /ramdisk/bin/php5 – you cannot guess it until you know it. If the hoster does not allow executing PHP from the command line. ask them how to use wget or lynx for this task. Then create your file, or files and have fun!

Hope this will help you. Let me repeat that’s not an ideal code and you might correct it for your own needs, just using the idea.