A Simple perl-based cron.php Fetcher for Drupal

I originally posted this on drupal.org here but I wrote it, so here it is.

I just ran into a fun problem: I installed a VMware LAMP appliance and it turns out it doesn't have lynx, links, curl, or wget. What to do? Luckily, it has perl. The following perl script grabs cron.php from localhost and prints the job's status to STDOUT.

require HTTP::Request;
require LWP::UserAgent;
$request = HTTP::Request->new(GET => 'http://localhost/cron.php');
$ua = LWP::UserAgent->new;
$response = $ua->request($request);
$code = $response->code($code);
$message = $response->message($message);
print STDOUT $code . " " . $message . "\n";

It doesn't get much simpler than this... you could replace 'http://localhost/cron.php' with $1 (no quotes needed, I think/hope) and then you could specify the URL on the command line.

I've tested this script, and it works. It should spit out "200 OK" if it worked. Otherwise it should give you the response code and the english explanation.

The crontab entry should look like the following:

0 0 * * * * /home/username/bin/cron.pl > /dev/null 2>&1

At least, if you want to run it on the hour, every hour. This is the standard for drupal, although if you have a lot of sites, space them out. If you want an email every hour telling you if cron.php completed or not (and why) then take off everything from the greater than (>) right (so it ends with .pl) and you'll get it - but I doubt you'll like it. It would be a good way to monitor a drupal site, though, as long as you didn't need more than one hour granularity - just send the email to your phone via SMS or to a pager via an email gateway.

If it doesn't run, make sure the first line of the script points to your perl location (lots of linux distributions have perl in /bin/perl and don't have a symlink to /usr/bin/perl like they would if they were intelligently designed) and also make sure that your script is set executable (chmod u+x cron.pl) The script does not need to run as root or any other user in particular.

I hereby release this script under the Gnu Public License, Version 2. No other version of the GPL may be applied to this code without my express written permission (I am the sole copyright holder.) You can find the GPLv2 below (as required.)

As a side note: While this will work on Windows, either using Cygwin's perl or ActivePerl there is an easier way - using wget. You can use Cygwin's wget, or you can download various other wget versions. My favorite can be downloaded here. Regardless, you use the following command:

schtasks /create /tn "Drupal Cron Job" /tr "c:\cygwin\bin\wget http://localhost/cron.php -O NUL:" /sc hourly

This uses the Microsoft Task Scheduler to do the job. We give it a reasonable name, and we tell it to run hourly, writing the file it downloads to NUL: so that it is swallowed up and vanishes, same as output to /dev/null on Unix. As you can see, I'm using cygwin's wget, but you can use any one you want, and I do suggest the one in the link above if you don't already have cygwin installed.

Plain text icon gpl.txt0 bytes

Add new comment