Docs for other versions: 2.6 / 2.5 / 2.4 / 2.3 / 2.2 / 2.1 / 2.0 / 1.9

Cron with UNIX

From MoodleDocs
Jump to: navigation, search

Cron with OS:

This page requires updating for Moodle 2.0. Please do so and remove this template when finished.


There are different command line programs you can use to call the Cron page from the command line. Not all of them may be available on a given server.

Note: The examples with wget, lynx, and similar are not the same as the "CLI only" cron checkbox, mentioned above (the configuration variable "cronclionly"). wget, lynx, and other similar utilities are Unix command-line HTTP clients, and thus running cron.php in this way is the same as running it in a browser, from Moodle's point of view.

wget

For example, you can use a Unix utility like 'wget':

wget -q -O /dev/null http://example.com/moodle/admin/cron.php

Note in this example that the output is thrown away (to /dev/null).

php

A number of users of Moodle have found that 'wget' sometimes fails. Especially if you have trouble with email digests not being sent on a daily basis to all users, an alternative command that solves the problem is:

php http://example.com/moodle/admin/cron.php

The same thing using lynx:

lynx -dump http://example.com/moodle/admin/cron.php > /dev/null

Note in this example that the output is thrown away (to /dev/null).

Alternatively, you can use a standalone version of PHP, compiled to be run on the command line. The disadvantage is that you need to have access to a command-line version of php. The advantage is that your web server logs aren't filled with constant requests to cron.php and you can run at a lower I/O and CPU priority.

/opt/bin/php /web/moodle/admin/cli/cron.php

Example command to run at lower priority:

 ionice -c3 -p$$;nice -n 10 /usr/bin/php /moodle/admin/cli/cron.php > /dev/null

Note: In version 2.0 and later the path for running cron.php from the commandline has changed. Use c:\moodle\admin\cli\cron.php

Using the crontab program on Unix

All that Cpanel does is provide a web interface to a Unix utility known as crontab. If you have a command line, you can set up crontab yourself using the command:

crontab -e

and then adding one of the above commands like:

*/30 * * * * wget -q -O /dev/null http://example.com/moodle/admin/cron.php

The first five entries are the times to run values, followed by the command to run. The asterisk is a wildcard, indicating any time. The above example means run the command wget -q -O /dev/null... every 30 minutes (*/30), every hour (*), every day of the month (*), every month (*), every day of the week (*).

The "O" of "-O" is the capital letter not zero, and refers the output file destination, in this case "/dev/null" which is a black hole and discards the output. If you want to see the output of your cron.php then enter its url in your browser.

For beginners, "EDITOR=nano crontab -e" will allow you to edit the crontab using the nano editor. Ubuntu defaults to using the nano editor.

Usually, the "crontab -e" command will put you into the 'vi' editor. You enter "insert mode" by pressing "i", then type in the line as above, then exit insert mode by pressing ESC. You save and exit by typing ":wq", or quit without saving using ":q!" (without the quotes). Here is an intro to the 'vi' editor.