The part that confuses most people about using cron for your Drupal installation is that the cron program is responsible only for the scheduling. You need a second program to actually call the cron.php file. wget, lynx, and curl are three candidates for this second program, and you are free to choose which one works best for you. You need to configure the program to call the URL of the cron.php file via HTTP It is important to allow the web server to handle the request instead of calling cron.php from the PHP command line, because the web server provides key information, such as the host name, that will otherwise be missing.
The easiest tool for sending an HTTP request to cron.php is wget (http://www.gnu.org/ software/wget/wget.html). To find out if you have wget available, issue the following command:
# whereis wget
If it is installed, you will see a line like this: wget: /usr/bin/wget
You can test that you have permissions to use it by issuing a command like this:
-S tells wget to print the output to the console, and - -spider tells wget not to retrieve the response, just the headers. The result should look like this:
# wget -S --spider drupal.org --16:53:14-- http://drupal.org/
=> ^index.html' Resolving drupal.org... 22.214.171.124 Connecting to drupal.org|126.96.36.199|:80... connected. HTTP request sent, awaiting response...
If you have gotten this far, wget is the best tool for you. You will most likely be able to use cron and wget to access cron.php in your Drupal installation. Here is the command to use to have wget call cron.php in the appropriate way:
/usr/bin/wget -q -O /dev/null http://www.yoursite.com/cron.php
The /usr/bin/wget part needs to be modified to match what was returned by the whereis wget command. The -q flag tells wget to be quiet (not to output its activities and status). The -O flag tells it to send whatever headers and output come from the web site to the destination that follows. In this case, the destination is /dev/null, which means the output will be discarded. Finally comes the correct URL to your cron.php file. Test the command in your shell, and then confirm from your Watchdog logs (Drupal path admin) that cron.php has actually run. If all is well, you will see the message "Cron run completed."
If you haven't had any success with wget, you can explore using the cron-lynx.sh or cron-curl.sh scripts in your Drupal's scripts directory. These depend on the lynx and curl programs, respectively. Refer to Drupal.org (http://drupal.org/node/23714) for further instructions. Alternatively, you might want to explore using a free web-based service, such as Webcron (http://www.webcron.org/).
After selecting the tool to call cron.php, you need to set up your cron schedule and task. A typical cron schedule looks like this:
The places correspond to minute, hour, day of the month, month, and day of the week, so this command would run at the thirtieth minute of every hour, every day, every month, every week. In other words, once an hour at 30 minutes past the hour. This is very much along the lines of what a typical Drupal site needs. But perhaps your site has more traffic, or uses a cron task to retrieve mail from the mail server to be published to the forums. In that case, you will want to run cron a little more often:
This runs the task every two minutes. Note that the varying parts are separated by a space. The cron task to run is specified alter the scheduling part. If you decide to use wget as the program to call cron.php, this is the command:
30 * * * /usr/bin/wget -q -O /dev/null http://www.yoursite.com/cron.php
Now you are finally ready to put the command in the crontab file. Use crontab -e, which will open your crontab file and insert the full command on a new line at the end. That should do the trick! Refer to cron's documentation (http://www.unixgeeks.org/security/newbie/ unix/cron-1.html) for more complete information.
Was this article helpful?