Drupal cron script

To avoid having to set up multiple cron lines - here's a script that will read your drupal install(s) and call cron on each one found.

Not sure where I saw the code (rss feed) - but the comment was present in the downloaded code - so thats a pointer to where it starts.

Changes I've made - the dir match is changed from ** to * so that it will follow symlinks. It also handles multiple drupal installs. Timeout wrapping added - one of my sites takes a long while to return.

#!/usr/bin/ruby

# Initial draft version by D'Arcy Norman dnorman@darcynorman.net
# Idea and some code from a handy page by (some unidentified guy) at http://whytheluckystiff.net/articles/wearingRubySlippersToWork.html

require 'net/http'
require 'timeout';

# Set up the locations of all drupal installs to be handled - no trailing /
drupalinstalls = ['/opt/drupal-4.7.4', '/opt/drupal-5.0']

# Loop thru them
drupalinstalls.each do |drupalsitesdir|
  # Get the settings.php file
  Dir[drupalsitesdir + '/sites/*/settings.php'].each do |path|
    File.open(path) do |f|
      f.grep( /^\$base_url = / ) do |line|
        line = line.strip();
        baseurl = line.gsub('$base_url = \'', '')
        baseurl = baseurl.gsub('\';', '')
        baseurl = baseurl.gsub('  // NO trailing slash!', '')

        # Found a baseurl for this site
        if !baseurl.empty?
          cronurl = baseurl + "/cron.php"

          if !cronurl.empty?
            begin
              # Adjust this timeout - lower is better but make sure it is high enough for your sites
              timeout(120) do
                url = URI.parse(cronurl)
                req = Net::HTTP::Get.new(url.path)
                res = Net::HTTP.start(url.host, url.port) {|http|http.request(req)}
              end
            rescue TimeoutError
              puts "Timed Out on " + baseurl
            end
          end
        end
      end
    end
  end
end

Feel free to comment with fixes or suggestions to make it better.

Technorati Tags:
worried