Configuring cron jobs

Cron is a daemon that executes commands at specified intervals. These commands are called "cron jobs." Cron is available on Unix, Linux and Mac servers. Windows servers use a Scheduled Task to execute commands.

There is a video, How To: Setting up Drupal's Cron that talks about cron and shows various ways of configuring it.

The cron command

In the following example, the crontab command shown below will activate the cron tasks automatically on the hour:

0 * * * * wget -O - -q -t 1 http://www.example.com/cron.php

In the above sample, the 0 * * * * represents when the task should happen. The first figure represents minutes – in this case, on the "zero" minute, or top of the hour. (If the number were, say, 10, then the action would take place at 10 minutes past the hour.) The other figures represent, respectively, hour, day, month and day of the week. A * is a wildcard, meaning "every time."

The rest of the line basically tells the server to "ping" the url http://www.example.com/cron.php.

Here is a diagram of the general crontab syntax, for illustration:

# +---------------- minute (0 - 59)
# |  +------------- hour (0 - 23)
# |  |  +---------- day of month (1 - 31)
# |  |  |  +------- month (1 - 12)
# |  |  |  |  +---- day of week (0 - 7) (Sunday=0 or 7)
# |  |  |  |  |
  *  *  *  *  *  command to be executed

Thus, the cron command example above means "ping http://www.example.com/cron.php at the zero minute on every hour of every day of every month of every day of the week."

How Drupal uses cron

Every Drupal install requires regular actions to handle maintenance tasks such as cleaning up log files and checking for updates. Cron.php is the file that Drupal uses to run the maintenance process.

For instance, if your site were www.example.com, loading the URL http://www.example.com/cron.php in your browser would run the maintenance.

This page is automatically set up when you install Drupal. Simply loading the URL will run the maintenance. Nothing more is required.

For a modest personal site, you might set up this cron job to run once a day. For a more active site you might want to run that job more often—perhaps every few hours or every hour. This regular visit will tell Drupal to perform its periodic tasks, and this will help keep your system running smoothly.

How to set up a cron job

Cron jobs are scheduled by setting up a "crontab." A crontab is a text file that contains the commands to be run. This file can be created and edited either through the command line interface, or, if you manage your website through a web-based control panel such as cpanel or Plesk, you will use the web interface. Check with your hosting company for detailed instructions if you are using a web-based control panel.

To edit a crontab through the command line, type:

crontab -e

If this fails, see the Troubleshooting Cron section below.

Add ONE of the following lines:

45 * * * *  /usr/bin/lynx -source http://example.com/cron.php
45
* * * * /usr/bin/wget -O - -q -t 1 http://www.example.com/cron.php
45
* * * * curl --silent --compressed http://example.com/cron.php

This would have a lynx, wget, or curl visit your cron page 45 minutes after every hour.

Three options are provided in case either wget, lynx or curl are not installed on the server. Any will do the job well.

Learn more about the crontab file syntax here to set up the cron job to run more or less often.

There are many ways to configure a cron job. If you have full access to crontab features, you should be able to simply paste in one of the above example commands – be sure to replace "example.com" with your own web domain or docroot.

If you're on shared hosting, you should be able to find cron job configuration somewhere in your hosting control panel. Some hosts even have cron "wizards" that walk you through the cron configuration, making it much easier if cron is new to you. On a Windows system you can accomplish the same thing with scheduled tasks to launch Internet Explorer pointed to the URL.

Some hosting companies don’t allow access to cron

If your hosting company restricts access to cron you have many options.

  • Ask the company to give you access, or to set up a cron job for you
  • Ask someone else with access to a server to set up a cron job for you. Any Unix, Linux, or Mac server with access to the internet can have a cron job to regularly visit your site.
  • Use the Poor Man's Cron module.

Cron doesn't guarantee your commands will run at the specified interval. But Drupal will try its best to come as close as possible. The more you visit cron.php, the more accurate cron will be.

Troubleshooting cron jobs

If you receive a permission denied error after starting crontab -e, you may need to use sudo:

sudo crontab -e

You may need to adjust the path to wget, lynx or curl in your crontab. For example, the cron example listed above contains the line:

45 * * * *  /usr/bin/lynx -source http://example.com/cron.php

However, Lynx may be in a different location on your server, or not installed at all. To find out where Lynx is installed, enter:

whereis lynx

or

which lynx

If it is not located at /usr/bin/lynx adjust the path as needed. The same applies for wget and curl. If none are installed ask a server administrator for help.

It may be necessary to change http://example.com/cron.php to the location of your Drupal installation. For example, if you have Drupal installed in a subdirectory, it might be http://www.example.com/drupal/cron.php).

Example scripts

Drupal ships with two example scripts in the scripts directory, cron-curl.sh and cron-lynx.sh. You can call these scripts from cron as well:

45 * * * * /home/www/drupal/scripts/cron-lynx.sh

Note that the scripts will need to be updated with the path to your directory and URL.

Security notes

It is possible to run cron.php directly via scripts/drupal.sh with Drupal 6. Drupal.sh allows a Drupal page to be executed from a shell script. To do so, add the following cron job to run as the Apache user.

/full/path/to/drupal.sh --root /full/path/to/site/root/ http://default/cron.php

Note that http://default/cron.php is NOT shown as an example, it should be used as is, without changes.

Taking this approach allows cron.php to be blocked to prevent remote access.

To block remote access to cron.php, in the server, .htaccess or vhost configuration file add this section:

    <Files "cron.php">
        Order Deny,Allow
        Deny from all
        Allow from localhost
        Allow from 127.0.0.1
        Allow from xx.xx.xx.xx <-- your IP address
    </Files>

If you take this approach and use drupal.sh to call cron.php, it is probably best not to use the root user to run the cron job. A non-privileged user account, or the Apache account user, for example http-service or www-data, is a better choice. To do so, call crontab -e when logged in as a non-privileged user, or for the Apache account on a Debian server, for example, you can add a user parameter:

sudo crontab -e -u www-data

Tips and tricks

Cron Sandbox is a useful site for testing cron entries.

Multiple sites

If you run many sites, you can use this tip to make managing your cron jobs easier. To minimize the clutter, create a /etc/cron.5min directory and have crontab read this directory every five minutes.

*/5 * * * * root run-parts /etc/cron.5min

Then place multiple individual files into the /etc/cron.5min directory, one for each site. The files can be named "site1", "site2", etc. -- note that run-parts may fail to detect files which contain a dot (.) in their name. To make sure that all of your files are visible to cron, type this at a shell prompt:

$ sudo run-parts --test /etc/cron.5min
and make sure that all of your files are listed.

Each of the files in /etc/cron.5min should contain one line:

/usr/bin/lynx -source http://(full site URL)/cron.php > /dev/null 2>&amp;1
or, alternatively, one of the curl or wget commands specified above.

If this doesn't work, try putting another line at the start of each file:

#!/bin/sh
and make sure that the files are executable by doing

$ sudo chmod u+x /etc/cron.5min/*

For more information about using cron in a multisite configuration, see the Multisite Cron section of this guide.

SSL

When using SSL, add one additional argument when calling wget: --no-check-certificate. Do not put “--no-check-certificate” between the -0 and the -.

45 * * * * /usr/bin/wget --no-check-certificate --quiet -O - https://example.com/cron.php

Configuring an editor for cron

You can specify which text editor (emacs, vi, nano, etc.) you want to use to edit the crontab. To tell the system which editor you want to use, type:

export EDITOR=nano

You need change cronjob to

winterheart - February 5, 2009 - 09:20

You need change cronjob to avoid useless recieving mail from cron:

0 * * * * wget -O - -q -t 1 http://www.example.com/cron.php > /dev/null 2>&1
_______
From Siberia with Love!

Name resolution, virtual domains and NAT

kokamomi - March 24, 2009 - 12:56

If you'r web server is on a machine behind a firewall/router that does NAT and therefore is on a private IP address, you cannot setup your cronjob pointing at the publicly visible domain name of your site, because unless you make special name resolution arrangements locally the cron script will not be able to reach the private IP address that your server is binding on. If the web server only serves one domain name or uses IP based virtual hosting you can simply point the cron job to the correct private IP address instead of the domain name.

If, on the other hand, the web server is set up serving name based virtual domains, that solution won't work. The web server has no way of nowing what domain to serve on your request for a specific IP. In this case you need to set up split horizon DNS or edit the local hosts file to associate your domain name locally to the private IP.

Wrong link ?

steff2009 - May 9, 2009 - 13:03

I would like to see a cron example but it seems that the link specified above http://default/cron.php returns an error and redirects to www.default.com

Hi, steff2009, that IS the

beanluc - June 3, 2009 - 14:22

Hi, steff2009, that IS the example. If you know how to use drupal.sh, then give it this command

/full/path/to/drupal.sh --root /full/path/to/site/root/ http://default/cron.php

Replace /full/path/to/ and /full/path/to/site/root/ with your actual directories.

Don't replace http://default/cron.php. It's not supposed to be any real website. It's an argument to drupal.sh.

Cron and HTTPS

nowl27 - June 11, 2009 - 08:00

I just figured out why my cron job wasn't running for Drupal - cron doesn't work if you're using HTTPS to get around your Internet provider blocking port 80 (HTTP). Fortunately, since I'm running it on my own server and have full access, this

0 * * * * wget -O - -q -t 1 http://www.example.com/cron.php

can be changed to

0 * * * * wget -O - -q -t 1 http://192.168.1.2/cron.php

to point to the local IP address.

Hopefully this will save someone some hair-pulling.

Just a quick note to add that

ezraw - June 11, 2009 - 14:06

Just a quick note to add that will not work if you are using virtual hosts running on the same IP.

Beware when using

ceardach - June 11, 2009 - 17:36

Beware when using http://default/cron.php

/full/path/to/drupal.sh --root /full/path/to/site/root/ http://default/cron.php

Areas that have $base_url will be rewritten to have default as the path, instead of the actual domain. This has serious concerns when sending email during cron, as all URLs in the emails will be wrong.

FreeBSD related cron

matreides - June 17, 2009 - 20:05

FreeBSD comes bundled with fetch which can also be used to schedule this process. I have added the following entry and believe it is working.

# Perform daily/weekly/monthly maintenance.
0 * * * * fetch -q http://drupal.techally.com/cron.php

Cheers,
Mikel

 
 

Drupal is a registered trademark of Dries Buytaert.