Drupal

Drupal and Cron

Drupal logoIf you run a website with Drupal content, you understand the need to run cron periodically (http://yoursitename/cron.php). Some hosting companies won't allow cron jobs on their servers, leaving you in a bind. If you have a Mac sitting around that's awake at a certain time every day, just have it and its built-in scheduling system, launchd, do the job for you.

  • If you don't have a text editor like BBEdit, download Bare Bones Software's free TextWrangler
  • Open a new doc in TextWrangler, and paste in this code: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>com.darla.cron</string> <key>ProgramArguments</key> <array> <string>/usr/bin/curl</string> <string>--connect-timeout</string> <string>10</string> <string>http://www.someurl.com/cron.php</string> </array> <key>StartCalendarInterval</key> <dict> <key>Hour</key> <integer>8</integer> <key>Minute</key> <integer>0</integer> </dict> </dict> </plist>
  • Modify this line
    <string>com.darla.cron</string>
    so the text com.darla.cron is the same as the name you are going to give the file (see naming file step), without the ending .plist.
  • Modify this line
    <string>http://www.someurl.com/cron.php</string>
    so that www.someurl.com is your site's name. (Drupal will need cron.php on the end!
  • Modify this text
    <dict> <key>Hour</key> <integer>8</integer> <key>Minute</key> <integer>0</integer> </dict>
    so the start time is what you want. This represents 8 am.
  • Save the file to ~/Library/LaunchAgents, with a name such as com.yourname.whatitdoes.plist, where ~/ is your "home" directory (Macintosh HD/Users/yourname). It should save with the correct permissions. (You don't need superuser or root permissions.) You must have the ending plist!
  • Open Terminal (Applications/Utilities).
  • Type this command (and don't press Enter until I tell you to!):
    launchctl load
    Then drag the plist file you created near the end of the command you just type. This will load the path and name of your plist. NOW press Enter. (There is no need for sudo. As a matter of fact, using sudo will generate an error!)
  • The Console (Applications/Utilities) will show any errors. Click on the System log, then in the search field, type in curl. This will filter the results for your curl command.

What does the above plist do? It invokes the Mac's scheduling system, launchd, at a specified time, and tells it to access a certain URL (link) via the curl command. That's all. Drupal has a built-in web page, cron.php, which performs certain maintenance tasks for your Drupal site. You could access the URL manually yourself once a day (or several times a day, depending on how active a site you maintain), but automation is so much more geeky.

Syndicate content