Note: You are currently viewing documentation for Moodle 2.1. Up-to-date documentation for the latest stable version is available here: Cron with Unix or Linux.

Cron with Unix or Linux: Difference between revisions

From MoodleDocs
Line 46: Line 46:
This will bring up an editor window (the first time it may ask you which editor to use). Add the command onto the end of the file (it may be empty or it may have some instructional comments):
This will bring up an editor window (the first time it may ask you which editor to use). Add the command onto the end of the file (it may be empty or it may have some instructional comments):
<pre>
<pre>
  */30 * * * * wget -q -O /dev/null <nowiki>http://example.com/moodle/admin/cron.php</nowiki>
  */15 * * * *       /usr/bin/php /path/to/moodle/admin/cli/cron.php
</pre>
</pre>
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 first five entries are the times to run values, followed by the command to run. This says to run the command every 15 minutes which is normally ok. In some cases you may wish to run it more often on a hosted system you may get complaints if you do not run it a lot less often (e.g. to run every two hours use '0 */2 * * *' for the first five entries)


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.


* [http://linuxweblog.com/node/24 A basic crontab tutorial]  
* [http://linuxweblog.com/node/24 A basic crontab tutorial]  

Revision as of 19:42, 2 December 2011

On Unix and Linux use the built in cron program which is standard on nearly all systems. You are required to add a command to the 'crontab' (the table that holds cron commands) for the web server user.

There are two different methods that can be used to invoke the Moodle cron process:

NOTE: The commands shown need to be added to the crontab to function (described in a moment). However, you can - and should - run them on the command line to check they work first.

The command line (cli) cron

If you have the PHP CLI version installed then this is the recommended method of invoking cron. The correct command is as follows...

/usr/bin/php /path/to/moodle/admin/cli/cron.php

(substitute the correct path to moodle and for php as required)

NOTE: This path is different to that used in Moodle 1.9 and earlier. If you are upgrading from 1.9 you will need to change your cron script path.

Web based cron

NOTE: In order to use the web based cron script you must first check Cron settings to make sure this method is enabled.

The idea is to call the following web page (you can try this from your browser):

http://url.of.your/moodle/admin/cron.php

A command line (text based) browser is needed to run this from the server. Possibilities are as follows...

wget -q -O /dev/null/ http://url.of.your/moodle/admin/cron.php

(no output is displayed - remove the -O /dev/null/ to test)

...OR...

curl http://url.of.your/moodle/admin/cron.php -o /dev/null/ -silent

(no output is displayed - remove the -o /dev/null/ -silent to test)

Using the crontab program on Unix/Linux

Once you have selected an appropriate command to invoke the Moodle cron it must be added to the web users 'crontab' to schedule it to run regularly. Use the following command (as root) substituting the correct user in place of 'www-date' (e.g. 'apache' for Centos, 'www-data' for Debian/Ubuntu - Google will know!)

crontab -u www-data -e

This will bring up an editor window (the first time it may ask you which editor to use). Add the command onto the end of the file (it may be empty or it may have some instructional comments):

 */15 * * * *        /usr/bin/php /path/to/moodle/admin/cli/cron.php

The first five entries are the times to run values, followed by the command to run. This says to run the command every 15 minutes which is normally ok. In some cases you may wish to run it more often on a hosted system you may get complaints if you do not run it a lot less often (e.g. to run every two hours use '0 */2 * * *' for the first five entries)


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.