Posted by deanloh on July 23, 2010 at 5:49am
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
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.
<?phpforeach($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
Thanks aliasghar for the advice.
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?
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
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
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
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
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.The Boise Drupal Guy!
Poor person's aegir
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
Does this do anything that Drush 3's mutli-site handling (as mentioned in my previous comment) doesn't?
The Boise Drupal Guy!
Probably not
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 !!!
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 cronThis 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/site1drush cron
then call the site1cron.sh in the crontab !
--
Twitter : @ismaeil_
With Drush 3, this sort of
With Drush 3, this sort of thing is no longer necessary. One may simply run:
to run the Drush
croncommand across all sites.The Boise Drupal Guy!
Will this balance running
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
I don't know what you mean by "balance," but it will run cron on all sites, one site at a time.
The Boise Drupal Guy!
load balanced cron
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-cronAnd, 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.