Drupal coder

Spread your cron schedule on machines running multiple Drupal sites

Here's a quick tip for people running multiple Drupal instances on one server: don't run cron for all sites on the same moment but try to spread it.

Suppose I have 5 sites: site1.tld, site2.tld, ... and I want to run cron each 12 hours. I would enter the following using crontab:

# m h  dom mon dow   command
* */12 * * * /usr/bin/wget -O - -q http://site1.tld/cron.php > /dev/null
* */12 * * * /usr/bin/wget -O - -q http://site2.tld/cron.php > /dev/null
* */12 * * * /usr/bin/wget -O - -q http://site3.tld/cron.php > /dev/null
* */12 * * * /usr/bin/wget -O - -q http://site4.tld/cron.php > /dev/null
* */12 * * * /usr/bin/wget -O - -q http://site5.tld/cron.php > /dev/null

This will result in all cron script to run at the same time causing spikes in your CPU usage (see graph).

unspread.jpg

To spread the usage in this case (running cron each 12 hours), run cron for the first site at 00:00 and 12:00. Run cron for the second site at 01:00 and 13:00, and so on. This results in the following crontab:

# m h  dom mon dow   command
* 0,12 * * * /usr/bin/wget -O - -q http://site1.tld/cron.php > /dev/null
* 1,13 * * * /usr/bin/wget -O - -q http://site2.tld/cron.php > /dev/null
* 2,14 * * * /usr/bin/wget -O - -q http://site3.tld/cron.php > /dev/null
* 3,15 * * * /usr/bin/wget -O - -q http://site4.tld/cron.php > /dev/null
* 4,16 * * * /usr/bin/wget -O - -q http://site5.tld/cron.php > /dev/null

Looking at the CPU usage graph the peaks are much smaller now.

spread.jpg

March 05, 2010cron, Drupal, performance

Comments

Great minds think alike! I recently wrote an article on automating Drupal site provisioning using Puppet, and included a recipe which automatically spreads the cron jobs for each site according to a hashing algorithm:

Puppet Drupal recipes

Of course, Puppet can do lots of other helpful things too using Drush to automate Drupal - let me know if you find the piece interesting.

You could also put the commands into a shell script so they are run consecutively instead of all at the same time. If you had definite slow periods of site activity it would be good to get them all done and out of the way

# m h dom mon dow command
* */12 * * * /usr/local/bin/rundrupalcrons.sh > /dev/null

In /usr/local/bin/rundrupalcrons.sh:
#!/bin/sh
/usr/bin/wget -O - -q http://site1.tld/cron.php
/usr/bin/wget -O - -q http://site2.tld/cron.php
/usr/bin/wget -O - -q http://site3.tld/cron.php
/usr/bin/wget -O - -q http://site4.tld/cron.php
/usr/bin/wget -O - -q http://site5.tld/cron.php

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options