Drupal 6: Dynamically execute all Drupal cron jobs for an entire server

You may have a lot of Drupal sites installed on the same server. Instead of creating a cron job for each individual site, you could write a script like this to loop through your sites and execute each cron job automatically. Here’s the script I created using PHP, lynx, and find:

#!/usr/bin/php

<?php

// define where vhosts exist
$sitesDir = '/var/www/vhosts';

// change dir
chdir($sitesDir);

// get a list of directories
// Pipe 1: get all directories
// Pipe 2: remove "./" from beginning of each line
// Pipe 3: remove "."
$command = "find . -maxdepth 1 -type d | sed 's/^\.\///' | sed 's/^\.$//'";

// execute command
$dirs = `$command`;

// convert string into array
$dirs = explode("\n", trim($dirs));

// loop through directories
foreach ($dirs as $d) {
  // ensure cron.php exists
  if (file_exists($sitesDir . '/' . $d . '/httpdocs/cron.php')) {
    $command = "/usr/bin/lynx -source http://$d/cron.php > /dev/null 2>&1";
    $output = `$command`;
  }
}

?>

I then added the following cron job to root:

#min    hour    dMonth  month   dWeek   command
*/5     *       *       *       *       ~/scripts/drupal-crons.php

Updated: