Cron script for multi source, multi site setup
Last modified: March 22, 2008 - 15:44
I've just hacked up this little script as I have multiple installations of drupal each with multiple sites that get more each day. This should figure out which sites cron should run for.
#!/bin/bash
SITESROOT=/var/www/sites
MYIPRANGE=41.204.221
# get the base installs
cd $SITESROOT
for drupaldir in $(find . -maxdepth 2 -name INSTALL.mysql.txt | awk -F/ '{print $2}')
do
cd $SITESROOT/$drupaldir/sites
for site in $(find -L . -maxdepth 1 -type d -iregex "./[a-z].*\.[a-z].*" | awk -F/ '{print $2}')
do
IP=$(dig $site | sed '/AUTHORITY SECTION/,$d' | grep -v "^;" | grep "IN[[:space:]]*A" | sed 's/.*A\W*//' )
if echo $IP | grep -q $MYIPRANGE
then
#echo "Doing cron for $site"
wget -O - -q http://$site/cron.php
else
a=1
#echo "Skipping cron for $site"
fi
done
done
Why did you add the IP-range
Why did you add the IP-range that you use? Is it because every Drupal-site (domain) in your multisite setup uses it's own unique IP adress?
If I have only 1 IP adress for all the sites in the multi-site setup, what do I enter in the IP-range line? The last 3 digits of the IP adress or the entire IP adress?
A more simple bash script
The script above works, but is a little over-engineered for an environment where you mostly trust the contents of your 'sites' directory to be accurate. It also depends on 'dig' which is not available on every server (I tend to not install it on mine).
This version of the script does a few things differently.
The one case where I'm unsure how the script will behave looks like:
The domain (foo.example.com) is pointing at your server and you have a directory by the name 'foo.example.com' in your sites directory, and you have not configured apache to point at the drupal multi-site installation for incoming foo.example.com requests. This should not stop the others from running, I give the warning because I have not tested this case. Expected cases work, and domains not pointed at the server do not work.
Put this in a file with a .sh extension, and call that from your crontab.
#!/bin/bash
# This script will iterate through the sites directory of a multi-site install
# and run the cron.php for each named site in the directory.
# NOTE: the site defined in 'sites/default' must have its URL set statically here.
# set the domain of the site defined in 'sites/default'
# comment these lines out if you don't need or use them.
# DEFAULTDOMAIN=www.example.com
# wget -O - -q http://$DEFAULTDOMAIN/cron.php
# set the system path for the multi-site sites directory
SITESROOT=/var/www/drupal-5.10/sites
# set the IP of your server
MYIPRANGE=192.168.1.101
cd $SITESROOT # work in the right dir
for site in $(ls |egrep -v "all|default")
do
if ping -c 1 $site |grep -q $MYIPRANGE
then
wget -O - -q http://$site/cron.php else
fi
done