Setting up Crontab for Drupal

Cron is an essential part of the Drupal CMS. It is used for things like retrieving syndicated RSS feeds from other sites and syndicating infomation from your own site. The cron feature is also used by many other modules to handle required background processing.

This article will explain how to correctly set up your site so you can schedule the cron daemon.

System Requirements:

  • Details will be based on the standard Drupal CMS install. (Currently 4.6.x)
  • Server OS: Linux
  • Server: Apache
  • Website Backend: CPanel

The Cron System for Drupal consists of 3 parts:

  • a script to call the Drupal cron page
  • the Drupal cron page
  • the cron daemon itself (Apache)

The Cron Script

Drupal comes with at least two versions of the cron script. Both can be found in the scripts directory. One of the scripts is written for Curl (cron-curl.sh) and the other, the one that I am using, is written in Lynx (cron-lynx.sh).

Step 1: Edit the script:

The following line should already exist in the script:
/usr/bin/lynx -source http://yoursite.com/cron.php > /dev/null 2>&1

Edit this line to reflect the url of your website:
/usr/bin/lynx -source http://somesite.com/cron.php > /dev/null 2>&1

Be sure to save the script after you have finished editing.

Step 2: Upload and Set Permissions

If the script is not already on the server, upload it to the server (I used FTP). Now we need to set the file permissions of the script.

Important: If the permissions are not set correctly, the cron daemon will not be able to execute the script!

The permissions for the script need to be 755 which gives execute permissions to User, Group, and World.

Using the CPanel File Manager, we browse to the scripts directory and click on the cron-lynx.sh filename. In the right hand column, a list of options appears for the file:

Show File
Delete File
Edit File
Change Permissions
Rename File
Copy File
Move File

We want the Change Permissions option so click on that item.
A table should appear that contains the cron-lynx permission settings. The following items should be checked:

Read: User, Group, World
Write: User
Execute: User, Group, World

After checking the correct boxes, click the Change button. If the settings are correct, the Permissions row will now read 7, 5, 5.

The permissions can also be set from the command line (assuming that you have command line access) by using chmod. For further information regarding chmod, a good tutorial can be found at http://catcode.com/teachmod/.

The Cron Daemon

Now that we have the neccesary scripts setup, we need to tell the cron daemon what to execute, and when to execute it.

Step 1: Go to the Crontab Editor

On the CPanel home page, find the item labeled Cron Jobs. We are presented with a choice of editors. Select the Advanced (Unix Style) button.

This is the Standard Cron Manager page.

Step 2: Tell the daemon what script to run

The crontab program needs to know the path and filename of the script to run. This path must be a file system path! A relative path based on the website’s URL will not work.

My server has an alias set to the base path of my web page directory. Since this is based on a file system path, this is what I use. You will need to be sure to use a complete file system path or find out what the alias is for your web page directory.

My alias is ~, and my web pages are in the www directory, so my complete path to the cron script is: ~/www/scripts/cron-lynx.sh. This path should be placed in the Command text field.

Step 3: Tell the daemon when to run the script

Scripts can be run as often as every minute. They can also be set to run just once a year if you choose.

When deciding how often to run your script, keep in mind that the crontab program does use up server bandwidth. Only run the script as often as neccesary! I prefer to run the script no more often than every 5 minutes. I have not found any reasons to run cron this often though. Currently, my script is called hourly.

When scheduling intervals, * means that crontab runs at every possible interval for that field. Looking at the Cron Manager page, you’ll notice fields for Minute, Hour, Day, Month, and Weekday. If all of the fields are set to *, the crontab will call the script every minute of every day of every month. If numbers are supplied, crontab handles these as times. 0 0 * * * will call the script once at midnight every day.

To set crontab to run at intervals, use the following format: */n where n is the time frame (relative to the field it’s in).

Some examples:
*/5 * * * * : Run cron every 5 minutes
* */1 * * * : Run cron every hour
* * */3 * * : Run cron every 3 days
* * * */1 * : Run cron once every month

*/5 * */3 * * : Run cron every 5 minutes, all day, every 3rd day.

30 0 * * * : Run cron at 12:30 every day

For further information and a helpful form to generate crontab intervals, check out http://htmlbasix.com/crontab.shtml.

My current settings are:
0 */1 * * *
This calls my script at the top of every hour.

Verify Cron is Set Up Correctly

We want to make sure that the crontab is working correctly. After we have verified this we will set it up as it should run on a day to day basis.

Step 1: Set a Test Interval

To verify that your cron is set up correctly, set the interval to run every minute (so we don’t have to wait).

*/1 * * * *

Click the Commit Changes button, and make sure CPanel says Cron Updated.

Step 2: Verify Cron Log

Log in to your site as administrator and go to administer > logs. If needed, select the cron messages message filter type and click Filter.

You should see something similar to the following message:
cron 09/06/2005 – 11:00 Cron run completed

If you see a ‘Cron run completed’ message, everything is working correctly!

If not,

  • Verify that the cron-lynx.sh script has the correct execute permissions.
  • Verify that the path to cron.php (in cron-lynx.sh is correct).
  • Verify that the filesystem path to the script is correct (in the Crontab Manager).
  • Verify that the interval is set correctly (in the Crontab Manager).

Step 3: Final Set Up

If everything is working correctly, go back to the CPanel Cron Manager and set the crontab interval to the desired setting. Commit the changes and enjoy the results.

Summary

We have walked through the neccesary steps to set up the cron feature. We edited the cron script to point to the cron page. The cron script permissions were set to the correct values so that the crontab program could access it. Next, we set up the crontab arguments to find and call the script. Finally, we verified that cron is getting called (working correctly).

The Crontab can be frustrating to set up due to a lack of in depth information. Hopefully, this How-To guide has set you on the right path and given you the information needed to accomplish this goal.