Configuring cron jobs
Some modules require regularly scheduled actions, such as cleaning up log files. Using cron can make these happen automatically.
Cron is a command scheduler that resides on your server and executes commands at intervals, specified in seconds. You can use it to control the execution of daily, weekly or monthly jobs (or anything with a period measured in seconds). These tasks are called "cron jobs."
What you want to do is schedule a "cron job" that tells a web browser on your server, such as lynx or wget, to regularly visit your "cron page." For instance, if your site were www.example.com, your cron page would be http://www.example.com/cron.php. (This page is automatically set up for you when you install Drupal.) This regular visit will automatically tell Drupal to perform its periodic tasks, and this will help keep your system running smoothly.
For a modest personal site to which you post now and then, you might set up such a cron job to run once a day. For a more active site you'd likely want to run that job more often--perhaps every few hours or every hour.
With Linux or Unix you schedule your cron jobs by setting up what's called a "crontab." (You might rely on helper programs like C-Panel to make setting up your cron jobs easier.)
Your crontab might look like this:
45 * * * * /usr/bin/lynx -source http://example.com/cron.php
or
45 * * * * /usr/bin/wget -O - -q http://example.com/cron.php
This would have a browser (lynx or wget) visit your cron page 45 minutes after every hour. (At the start of the hour your site may receive a lot of traffic from other sites aggregating your content via RSS. So running your cron job earlier helps the job avoid that traffic.)
To set up a cron job on Windows read configuring Windows cron jobs.
Suppose your hosting company doesn't allow you to set up crontab entries.
- You can ask the company to let you. Sometimes that's all you need to do.
- You can ask someone else with access to the internet to set up an entry for you. After all, virtually any Unix/Linux machine with access to the internet can set up a crontab entry to regularly visit http://example.com/cron.php.
- You can use a webcron service (for example, http://www.webcron.org), to set up cron jobs through a web interface.
- If you can't run cron yourself or find someone to do it for you, consider the poormanscron 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.
Some guidelines of a more technical nature:
Take a look at the example scripts in the scripts directory. Make sure to adjust them to fit your needs. For example, the cron-lynx.sh file contains the line:
/usr/bin/lynx -source http://example.com/cron.php > /dev/null 2>&1
Enter:
whereis lynx
from your shell to make sure lynx is located at /usr/bin/lynx (if not, adjust the path as needed). Also change http://example.com/cron.php to the location of your Drupal installation (for instance, it might be http://www.example.com/drupal/cron.php).
A good crontab line to run this cron script once every hour would be:
00 * * * * /home/www/drupal/scripts/cron-lynx.sh
Note that it is essential to access cron.php using a browser on the web site's domain; do not run it using command line PHP directly and avoid using localhost or 127.0.0.1 or some of the environment variables will not be set correctly and features may not work as expected (for example, the Drupal module).
If your host doesn't give you permission to use a browser such as wget or lynx, there is also a script in Drupal's /scripts/ directory called cron-curl.sh which uses curl instead. It's also possible to write a simple PHP script which simulates a browser accessing your website's cron.php (using curl, fopen, or similar), and write a cron configuration which calls that instead. It is also possible to issue the call to cron.php directly from cron as follows:
/usr/bin/lynx -source http://example.com/cron.php > /dev/null 2>&1
or
curl --silent --compressed http://example.com/cron.php > /dev/null 2>&1
Note that the > /dev/null 2>&1 sends both error messages and any output to /dev/null so that users setting up cron jobs won't be emailed with error reports or output.

Additional information
This was a helpful page, but it seems to be missing some information that I needed as a person who runs my own Linux server. You explained what cron was and how it works but you didn't explain what the crontab is, where to find it, and how to edit it (for Linux users).
DISCLAIMER: I only just did this process myself for the first time, so I may be completely full of crap about everything I tell you here, but I'm reasonably confident that this is mostly good information.
The crontab is actually a text-based file that contains a list of cron job scripts like the one listed below:
45 * * * * /usr/bin/lynx -source http://example.com/cron.phpCron executes the cron jobs listed in the crontab file however often they're supposed to be executed, based on the information in the script. At first, I didn't understand what the 45 * * * * meant, but here's a good webpage that explains it:
http://www.adminschoice.com/docs/crontab.htm
(basically, it's just a way to tell cron when to execute the script, such as hourly, daily, at midnight on the 1st of march, etc)
Typically, the crontab file can be found on a Linux server in /usr/bin directory and it's called, of course, crontab.
You must have root or sudo access to modify the file (no surprise there).
You must also specify which text editor program you want to use to edit the crontab. While most Linux people seem to recommend vi, I found it extremely unintuitive to use so I use nano (which is included in Ubuntu Linux, which I use). In any case, to tell the system which editor you want to use, type:
export EDITOR=nano(or type vi instead of nano or whichever text editor. You may have to use sudo for these various commands if you aren't already the root user.)
Then open the crontab file to edit it:
crontab -eThe editor you picked now opens the crontab file for editing. Depending on whether your crontab file is being used by any other programs, it may or may not be empty. In either case, you would arrow down to the first available blank area on the page and type in your new cronjob script:
45 * * * * /usr/bin/lynx -source http://example.com/cron.phpNow save your crontab file and you're done. The new line will be executed however often you specified.
Hope this was useful (and, more importantly, accurate).
UPDATE: Since I posted this message several hours ago I've reviewed my Drupal status reports and I'm happy to tell you that the cron.php is running exactly as scheduled every hour, so it works.
Cron on MAMP
To add on to VinceDee's instructions, this is how I got cron to run on MAMP:
Open terminal and type:
crontab -eThis opens your crontab file or creates a new one if you don't have one.
Using vi or whatever your default editor is, add the line
* * * * * curl --silent --compressed http://localhost/{your_site}/cron.phpChange the URL to be whatever you use to view your site locally.
Change the five stars as you wish your cron to run. ( * * * * * runs the cronjob every minute, rather too much for most uses.)
Save the file. (If your default editor is vi, this requires pressing ESC and then :wq)
2 Second Cron Job
Thanks a lot for this info. Doing Apache admin/own server for 1st time as well (Ubuntu). This is what worked for me.
whereis wgetFind the path to wget (if it's installed)
sudo crontab -eOpen crontab in default editor. Then add following line to bottom (crontab may be empty or have other jobs in it).
*/2 * * * * /usr/bin/wget -O - -q http://example.com/cron.phpAfter a few minutes went to /admin/logs/status (5.x) and /admin/reports/status (6.x) to make sure it was working and then changed */2 (to run every other minute) to 45 as in the example.
Hope that helps someone. I personally like the terminal cut-n-paste instructions myself, have saved notes for when I have time to learn all about CRON and lynx, wget, curl and root crontab vs. other user vs. referencing .sh in Drupal core etc later!!!
===
~mahalie
Security issues
Some notes, before you run a cronjob via a php-script that is in a web-dir path:
When you will not (and I think that should be in most cases) have a "really dynamic web-site" ;-), where some "Guests" manage your server, you should never run "cron.php" as root. I know, Drupal try to be very secure (one point because I use Drupal), but sometimes, ther are bugs in http-service, php or Drupal itself.
Ok, when the web-service runs in a chroot-enviroment, only this will be "hacked". You can still login via "ssh" ore something and fix the security-hole. But when you run the "cron.php" as root, the Hacker can change the code in it (change the root-password for example) and after the cronjob was running, he has full access to your server. Much more harder to fix that, because you have not the new password.
But much more harder is it, when the Hacker make something, what you don't notice in the first time... You now what I mean...
So use the user from http-service for the crontab is much better.
On Debian, for example:
sudo crontab -e -u www-data
....
Stefan.
Site5 Cron Job
I was able to get my cron job to run and be reported properly in the status reports by setting the crontab interface of multisite as follows:
php www/cron.php
I was not able to get the system help recommendation, using GET and the url to work properly, although I could run the script remotely from a browser.
/Len
WT6G
More Site5 and Cron
These both worked on one of the sub-domains of my multi-sites.
wget -O /dev/null http://www.sitename.com/cron.php >/dev/null
/usr/bin/wget -O - -q http://www.sitename.com/cron.php
But neither on my main site registered as the multi-site.
Crontab commands
Anyone who has to configure crontab scheduling commands ( the 'm h D M DoW' fields), might find it helpful to visit Cron Sandbox at HxPI. Type in the parameters and get a list of all the times cron would run your job in the coming days.
www.hxpi.com/cron_sandbox.php