One script to trigger cron jobs on multi-site

Events happening in the community are now at Drupal community events on www.drupal.org.
deanloh's picture

Looking for advice. I'm using the following script and set a cron to run it every hour. It's suppose to trigger cron job of all the sites I have set up on multisite. The list consists of couple of dozen of websites. I need to know, will this cause any problem to server load, especially when the list gets longer? Thanks in advance to any kind advice!

<?php
$cronUrl
= 'http://%s/cron.php';
error_reporting(E_ALL);
$sites = array();
$sites[] = 'mysite.com';
$sites[] = '1.mysite.com';
$sites[] = '2.mysite.com';
$sites[] = '3.mysite.com';
$sites[] = '4.mysite.com';
// and so on...

foreach($sites as $site){
   
$cmd = 'wget --spider '.sprintf($cronUrl, $site);
    echo
'Executing command: '.$cmd.'<br>';
   
exec($cmd);
}
?>

Comments

A simple trick

pendashteh's picture

i don't know about the pressure of running multiple cron; but using a simple trick you can distribute the load among the minutes of an hour to prevent running multiple cron jobs.

for example using the code below, every 5 minutes you can run one's cron.

<?php
foreach($sites as $i=>$site) {
$iMinute = (int) date('i'); // get the minute
   
if ($i*5 != $iMinute) {
        continue;
  }
 
$cmd = 'wget --spider '.sprintf($cronUrl, $site);
    echo
'Executing command: '.$cmd.'<br>';
 
exec($cmd);
}
?>

this code works up to 12 sites, but you just can play with it to achieve what you want.

(note: i don't know about preference of using wget over direct php but i know doing 'echo' in a cron job is not recommended.)

Thanks aliasghar for the

deanloh's picture

Thanks aliasghar for the advice.

i don't know about preference of using wget over direct php but i know doing 'echo' in a cron job is not recommended.

I got the script from another post on drupal.org, was oblivious of good/bad practice as long as it serves the purpose. Please share if you have better one that I can use. Currently I have close to 30 sites on multisite set up. Thanks again for your help!

Use the Latest Aegir?

jamiet's picture

If you have close to 30 sites in a multi-site setup you may want to consider using Aegir? The latest release of aegir (0.4 branch) runs the cron job of sites it looks after automatically based on it's own cron job setup. The latest branch is alpha but is very stable and makes managing sites a lot easier (dare I say fun ;) ).

JamieT

Thanks for the suggestion. I

deanloh's picture

Thanks for the suggestion. I was aware of what Aegir can do, only trouble is I didn't have the confidence and resource to set it up. Maybe one day I'll get a dedicated server and give it a try.

Aegir works on VPS as well

jamiet's picture

Aegir is getting easier to install upon each version and you don't need an expensive dedicated server you can install aegir on a VPS and if you shop around you can get one of them at a price comparable to shared hosting.

I've pm'd you so check that as well and let me know if I can help at all.

JamieT

in my expreiences the cron

pendashteh's picture

in my expreiences the cron job returns had either sent to my email or lead to create a file on the root directory and both was annoying.
but if a cron job return blank no mail would be sent and no file would be created.

I strongly recommend that you

Garrett Albright's picture

I strongly recommend that you don't use wget or similar tools to run cron jobs. Instead, use Drush. That way, you avoid problems with server load and timeout.

Once you're using Drush, you can either create new cron jobs (in the crontab) per site, or write a shell script which runs the cron job for each site in sequence, or, if you have a multisite set-up, just do drush @sites cron.

Poor person's aegir

skwashd's picture

I put together what I call "poor person's aegir", which is a simple wrapper for drush to execute a command across all of your sites. You can find it at http://davehall.com.au/blog/dave/2009/12/08/updating-all-your-drupal-sit...

You would just need to change it so drush runs cron rather than updatedb. Let me know if you need any help with it.

Does this do anything that

Garrett Albright's picture

Does this do anything that Drush 3's mutli-site handling (as mentioned in my previous comment) doesn't?

Probably not

skwashd's picture

My "poor person's aegir" was written before this feature was implemented in drush. It is what I still use. I suppose I should really update my crons to use the newer functionality. At the same time either method works and is acceptable.

Drush for crons ! or better: a drushall script !!!

izus's picture

Hi there !
I'm a member of a team that administrates 32 websites of students' associations and clubs, and for all these 32 websites, we use a drush based script to run crons (and many many other things !)

we have a file named : drshall.sh in ~/bin/

#~/bin/sh
if [ $# -lt 1 ]; then
  echo "usage: $0 <drush args>"
  exit 1
fi

cd [drupal directory]/sites

for x in $(ls -1 | grep -v 'all'); do
  if [ -d $x -a ! -L $x ]; then
    cd $x;
    echo $x
    drush $*
    cd -;
  fi
done

so to run crons for all sites, the command line is :

sh ~/bin/drushall.sh cron

This was for running cron at the same time for all sites

but if you want different periods for different sites, the script is easier !
site1cron.sh

cd ~/drupal/sites/site1
drush cron

then call the site1cron.sh in the crontab !

--
Twitter : @ismaeil_

With Drush 3, this sort of

Garrett Albright's picture

With Drush 3, this sort of thing is no longer necessary. One may simply run:

drush @sites cron

to run the Drush cron command across all sites.

Will this balance running

fgjohnson@lojoh.ca's picture

Will this balance running cron on all sites?
Does it run all crons at once or on one site at a time in a multisite?

I don't know what you mean by

Garrett Albright's picture

I don't know what you mean by "balance," but it will run cron on all sites, one site at a time.

load balanced cron

snovak's picture

I too would be interested knowing if there is a nice multisite cron script, if it's not drush. I decided run
drush @sites -quiet core-cron
And, I'm still sitting. waiting.
I should have run it without the quite flag first to see the progress.
But, If a script were written with some timeouts and reporting, I think that would be ideal.

Multisite

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

Hot content this week