DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Drupalcron - Remote Cron Runner For Drupal (Perl Script)

11.06.2009
| 4822 views |
  • submit to reddit
        drupalcron - v0.1 - 20091106 - created by mbalazs

Remotely runs cron on a Drupal* site and can be
inserted into crontab for automatization.

IMPORTANT!! READ!! ##########################################################

This script, drupalcron is licensed under GPL.
I have written this app to make my Drupal administration works easier.
I thought this would be useful to every Drupal site administrator who
can't run a cron on the host server due to limited permissions, so I
decided to publish it but I'll take no responsibility. USE AT YOUR OWN RISK!
No error-checks and debugging chains are implemented, so if it doesn't
work, I'm sorry. LWP (libwww-perl on Debian-like distros) is required!
###############################################################################

Please fill the %conf hash first!
        cookie: Path for the file cookies will be stored in
          cron: Full URL path to cron (can be found at admin/reports/status)
           url: The base URL to your site.
          user: Username with administrator rights
          pass: Password
        logout: Full URL path to logout

* Drupal is a registered trademark of Dries Buytaert

use strict;
use warnings;
use LWP;
use vars qw( %conf $ua $res );

%conf = (
    cookie => "/tmp/.drupalcron.cookies",
    cron => "http://www.example.com/admin/reports/status/run-cron",
    url => "http://www.example.com/",
    user => "mbalazs",
    pass => "szalabm",
    logout => "http://www.example.com/logout",
);

print "drupalcron v0.1 - by mbalazs - 20091106\n"; $| = 1;

print "connecting to $conf{url}... ";

$ua = new LWP::UserAgent(
	'cookie_jar' => { file => $conf{cookie}, autosave => 1 },
	'requests_redirectable' => [ 'POST', 'GET' ],
); chmod(0600, $conf{cookie});
print "done.\n";

###################################################

print "getting form_build_id... ";
my $res = $ua->get($conf{url});
my $formbid = $1 if($res->content =~ /name=\"form_build_id\"[^<>\"\']+id=\"(.+?)\"/);
print "$formbid\n";

###################################################

print "logging in to $conf{url} as $conf{user}... ";
$res = $ua->post($conf{url}, [
    'name' => $conf{user},
    'pass' => $conf{pass},
    'form_id' => 'user_login_block',
    'form_build_id' => $formbid,
]);
if($res->content =~ /admin/) {
    print "success.\n";
} else {
    print "fail. Check 'pass' field in %conf section.\n";
    exit(1);
}

###################################################

print "performing the cron command... ";
$res = $ua->get($conf{cron});
print $res->status_line . "\n";

print "logging out and cleaning up... ";
$ua->get($conf{logout});
unlink($conf{cookie});
print "done.\n";
exit(0);