Run cron.php without crontab: now it’s possible.

Dear drupal webmaster newbies, this is for you!

Now it’s possible to run cron.php regularily, without any access to the server or without any knowledge about servers and crontab!

Just put these simple PHP lines in a block of your Drupal site, better if mostly present, or create a new block with them:

<?php
$tmstmp=mktime();
$last=file_get_contents("lastcron.txt");
if(($tmstmp-$last)>(60*60*6))
{
$pntr=fopen('lastcron.txt', 'w');
if(fwrite($pntr, $tmstmp))
{
mail ("dest","cron OK","OK");
include ("cron.php");
}
else
{
mail ("dest","cron FAIL","FAIL");
}
fclose($pntr);
}
?>

Of curse replace the lines containing mail (“dest”, with your own email address instead of dest.

Then you have to create a text file (lascron.txt) filled with whatever you want (eg: 123456 is ok!) and upload it on the server in the same folder of cron.php (usually the root of your FTP access). Then set file permission to 666 (rw,rw,rw): I’ve done it with FileZilla, right click on the uploaded file, file attributes, put the flag on write permission for everyone… anyway … do it, no matter how 😉

Done!

Now ‘cron.php’ will run more or less regularly, depending on how frequently your site is visited and you’ll receive also an email notification.
In the sample I’m tryng to run it every 6 hours and not more frequently: the corresponding code for that is (60*60*6), that calculate the number of seconds in 6 hours.

Note that in the DB, ‘variable’ table, at the record where ‘name’ = “cron_last”, ‘value’ field contains a string that can be used instead of the timestamp taken fron the text file (first line mktime();). If you prefer you can modify the script…

5 thoughts on “Run cron.php without crontab: now it’s possible.

  1. This version is better.
    I’ve also corrected a mistake in the original version…

    $tmstmp=date(“Y-m-d”);
    $last=file_get_contents(“lastcron.txt”);
    if($tmstmp!=$last)
    {
    $pntr=fopen(‘lastcron.txt’, ‘w’);
    if(fwrite($pntr, $tmstmp))
    {
    mail (“dest”,”cron OK”,”OK”);
    include (“cron.php”);
    }
    else
    {
    mail (“dest”,”cron FAIL”,”FAIL”);
    }
    fclose($pntr);
    }

    1. sorry maybe it s a stupid question but there are differences between this solution or using poormancron module? could be that using poormancron the website slow down?
      thank you

      1. Hi, I don’t know exactly what “poormancron” does, but I think it shouldn’t slow down the website. If in doubt, just deactivate the module and try my method…

        Of course let us know!

  2. How the cron will run without setting to server and crontab?

    (60*60*6))
    {
    $pntr=fopen(‘lastcron.txt’, ‘w’);
    if(fwrite($pntr, $tmstmp))
    {
    mail (“dest”,”cron OK”,”OK”);
    include (“cron.php”);
    }
    else
    {
    mail (“dest”,”cron FAIL”,”FAIL”);
    }
    fclose($pntr);
    }
    ?>

    How the cron will run with just depends on the time set in the php script, like above? Can you provide any suggestion?

    1. Hi, the suggestion is inside the post. Read the whole post, may be you have seen only the preview version.

      “[…] ‘cron.php’ will run more or less regularly, depending on how frequently your site is visited and you’ll receive also an email notification. In the sample I’m tryng to run it every 6 hours and not more frequently: the corresponding code for that is (60*60*6), that calculate the number of seconds in 6 hours.[…]”

      Please, take a look also at the comment “better version”…

Leave a Reply

Your email address will not be published.