Note: You are currently viewing documentation for Moodle 2.3. Up-to-date documentation for the latest stable version is available here: Cron.

Cron: Difference between revisions

From MoodleDocs
m (inter-lang links)
 
(94 intermediate revisions by 38 users not shown)
Line 1: Line 1:
Some of Moodle's modules require continual checks to perform tasks. For example, Moodle needs to check the discussion forums so it can mail out copies of posts to people who have subscribed.
{{Installing Moodle}}
The Moodle 'cron' process is a PHP script (part of the standard Moodle installation) that must be run regularly in the background.   The Moodle cron script runs different tasks at differently scheduled intervals.


The script that does all this is located in the admin directory, and is called cron.php. However, it can not run itself, so you need to set up a mechanism where this script is run regularly (eg every five or ten minutes). This provides a "heartbeat" so that the script can perform functions at periods defined by each module. This kind of regular mechanism is known as a '''cron service'''.
'''IMPORTANT: Do not skip setting up the cron process on your server for your Moodle. Your site will not work properly without it'''


Note that the machine performing the cron '''does not need to be the same machine that is running Moodle'''. For example, if you have a limited web hosting service that does not have a cron service, then you can might choose to run cron on another server or on your home computer. All that matters is that the cron.php file is called regularly.
A special program (typically called - not surprisingly - 'cron') is used to run the Moodle cron script at a regular interval. The Moodle cron script runs tasks include sending mail, updating Moodle reports, RSS feeds, activity completions, posting forum messages and other tasks. Since different tasks have different schedules, not every task will run in Moodle when the cron script is triggered.  


The load of this script is not very high, so 5 minutes is usually reasonable, but if you're worried about it you can reduce the time period to something like 15 minutes or even 30 minutes. It's best not to make the time period too long, as delaying mail-outs can slow down activity within the course.
The cron program (that runs the Moodle script) is a core part of Unix based systems (including Linux and OSX) being used to run all manner of time-dependent services. On Windows the simplest solution is to create a task in the Windows Task Scheduler and set it to run at regular intervals. On shared hosting, you should find the documentation (or ask support) how cron is configured.  


First, test that the script works by running it directly from your browser: ''<nowiki>http://example.com/moodle/admin/cron.php</nowiki>''
Essentially, the task involves adding a single command to the list of cron activities on your system. On Unix based systems this list is a file called a 'crontab' which all users have.


Now, you need to set up some of way of running the script automatically and regularly.
== General discussion ==


==On Windows systems==
See the later sections for your server type; this section contains some general background information.


The simplest way is to use this little package [http://moodle.org/download/modules/moodle-cron-for-windows.zip moodle-cron-for-windows.zip] which makes this whole thing very easy by installing a small Windows service. Run it and forget about it! :-)
There are essentially two steps to implementing cron:
# identifying the correct command to run
# finding the right place on your system to put the command


==On web hosting services==
=== Working out the Moodle cron command ===


Your web-based control panel may have a web page that allows you to set up this cron process. For example, on Cpanel system, look for a button called "Cron jobs". In there you can put the same sort of Unix commands as listed below.
Moodle has two different ways to deploy cron which use different scripts within the Moodle install. These are as follows...
# The CLI (command line interpreter) script. This will be at the path '''/path/to/moodle/admin/cli/cron.php'''. If in doubt, this is the correct script to use. This needs to be run by a 'PHP CLI' program on your computer. So the final command may look something like '''/usr/bin/php /path/to/moodle/admin/cli/cron.php'''. You can (and should) try this on your command line to see if it works.
# The web based script. This needs to be run from a web browser and will be accessed via a web url something like '''http://your.moodle.site/admin/cron.php'''. You can find command line based web browser (e.g. wget) so the final command may look like '''/usr/bin/wget http://your.moodle.site/admin/cron.php'''. This has the advantage that it can be run from *anywhere*. If you can't get cron to work on your machine it can be run somewhere else.


==Using the command line on Unix==
=== Finding the right place to put the command ===


There are different command line programs you can use to call the page from the command line. Not all of them may be available on a given server.
This really does depend on the system you are using and you should find and read the documentation for your platform or hosting. In most cases getting the Moodle cron to run consists of establishing the correct command (above) and then adding it, and the time to run the command, to some sort of file. This might be either through a specific user interface or by editing the file directly.


For example, you can use a Unix utility like 'wget':
If using the CLI version you also need to make sure that the cron process is run as the correct user. This is not an issue with the web version.


wget -q -O /dev/null <nowiki>http://example.com/moodle/admin/cron.php</nowiki>
Example... installing cron on Ubuntu/Debian Linux. Assuming logged in as root..


Note in this example that the output is thrown away (to /dev/null).
''use the crontab command to open a crontab editor window for the www-data user. This is the user that Apache (the web server) runs as on Debian based systems''
<pre>
$ crontab -u www-data -e
</pre>
''This will open an editor window. To run the cli cron script every 15 minutes, add the line:''
<pre>
*/15 * * * * /usr/bin/php  /path/to/moodle/admin/cli/cron.php >/dev/null
</pre>
NOTE: the final '''>/dev/null''' sends all the output to the 'bin' and stops you getting an email every 15 minutes.


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:
== Setting up cron on your system ==
Choose the information for your server type:


php <nowiki>http://example.com/moodle/admin/cron.php</nowiki>
*[[Cron with Unix or Linux]]- Cron services on various UNIX and Linux flavored operating systems.
*[[Cron with Windows OS]] - Cron services in Windows
*''Apple OSX'' - use the built-in 'crontab' service which is exactly the same as [[Cron with Unix or Linux]]. However, you might want to do it the 'Apple way' using launchd - see [[Cron with MAC OS X]]
*[[Cron with web hosting services]]- Cron services in various web hosting examples.


Note in this example that the output is thrown away (to /dev/null).
Here are some more instructions for specific hosts (please check that these are up to date):
The same thing using lynx:


lynx -dump <nowiki>http://example.com/moodle/admin/cron.php</nowiki> > /dev/null
*[[Cron on 1and1 shared servers]]


Alternatively you could use a standalone version of PHP, compiled to be run on the command line. The advantage with doing this is that your web server logs aren't filled with constant requests to cron.php. The disadvantage is that you need to have access to a command-line version of php.
== Using third party cron service ==
Besides using cron hosted on your own server, you may use third party cron service (usually called webcron):


/opt/bin/php /web/moodle/admin/cron.php
*[https://www.easycron.com EasyCron] - A webcron service provider that eliminates the need of crontab or other task schedulers to set cron job.


==Using the crontab program on Unix==
=== Cron settings in Moodle ===


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:
There are settings within Moodle that control aspects of cron operation:


crontab -e
*[[Cron settings]] - Moodle cron process password and CLI settings


and then adding one of the above commands like:
===Remote cron===
Using the 'web based' version of cron it is perfectly ok to place the cron process on a different machine to the Moodle server. For example, the cron service on a Unix server can invoke the cron web 'page' on a Windows based Moodle server.


*/5 * * * * wget -q -O /dev/null <nowiki>http://example.com/moodle/admin/cron.php</nowiki>
==See also==
* [http://www.coursebit.net/moodle-cron-job/ All About the Moodle Cron Job]
* [http://en.wikipedia.org/wiki/Cron Wikipedia article on cron function]


Usually, the "crontab" 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).
Using Moodle forum discussions:
 
*[http://moodle.org/mod/forum/discuss.php?d=41827 Cron - can someone give me a quick confirmation of function?]
[[Category:Core]]
*[http://moodle.org/mod/forum/discuss.php?d=97684 Cronjob Question]
[[Category:Administrator]]
*[http://moodle.org/mod/forum/discuss.php?d=97457 Slow cron : avoiding simultaneous cron]
[[Category:Installation]]
*[http://moodle.org/mod/forum/discuss.php?d=117168 Visibility of cron.php]
*[http://moodle.org/mod/forum/discuss.php?d=139263#p609060 How to log the output of a Scheduled Task on Windows] - this discussion explains a nice trick that can be very useful when you are experiencing problems with your Windows Scheduled Task and you need to log the output of the Scheduled Task to a log file.


[[es:Cron]]
[[es:Cron]]
[[fr:Cron]]
[[fr:Cron]]
[[nl:Cron]]
[[ja:Cron]]

Latest revision as of 21:14, 13 October 2014

The Moodle 'cron' process is a PHP script (part of the standard Moodle installation) that must be run regularly in the background. The Moodle cron script runs different tasks at differently scheduled intervals.

IMPORTANT: Do not skip setting up the cron process on your server for your Moodle. Your site will not work properly without it

A special program (typically called - not surprisingly - 'cron') is used to run the Moodle cron script at a regular interval. The Moodle cron script runs tasks include sending mail, updating Moodle reports, RSS feeds, activity completions, posting forum messages and other tasks. Since different tasks have different schedules, not every task will run in Moodle when the cron script is triggered.

The cron program (that runs the Moodle script) is a core part of Unix based systems (including Linux and OSX) being used to run all manner of time-dependent services. On Windows the simplest solution is to create a task in the Windows Task Scheduler and set it to run at regular intervals. On shared hosting, you should find the documentation (or ask support) how cron is configured.

Essentially, the task involves adding a single command to the list of cron activities on your system. On Unix based systems this list is a file called a 'crontab' which all users have.

General discussion

See the later sections for your server type; this section contains some general background information.

There are essentially two steps to implementing cron:

  1. identifying the correct command to run
  2. finding the right place on your system to put the command

Working out the Moodle cron command

Moodle has two different ways to deploy cron which use different scripts within the Moodle install. These are as follows...

  1. The CLI (command line interpreter) script. This will be at the path /path/to/moodle/admin/cli/cron.php. If in doubt, this is the correct script to use. This needs to be run by a 'PHP CLI' program on your computer. So the final command may look something like /usr/bin/php /path/to/moodle/admin/cli/cron.php. You can (and should) try this on your command line to see if it works.
  2. The web based script. This needs to be run from a web browser and will be accessed via a web url something like http://your.moodle.site/admin/cron.php. You can find command line based web browser (e.g. wget) so the final command may look like /usr/bin/wget http://your.moodle.site/admin/cron.php. This has the advantage that it can be run from *anywhere*. If you can't get cron to work on your machine it can be run somewhere else.

Finding the right place to put the command

This really does depend on the system you are using and you should find and read the documentation for your platform or hosting. In most cases getting the Moodle cron to run consists of establishing the correct command (above) and then adding it, and the time to run the command, to some sort of file. This might be either through a specific user interface or by editing the file directly.

If using the CLI version you also need to make sure that the cron process is run as the correct user. This is not an issue with the web version.

Example... installing cron on Ubuntu/Debian Linux. Assuming logged in as root..

use the crontab command to open a crontab editor window for the www-data user. This is the user that Apache (the web server) runs as on Debian based systems

$ crontab -u www-data -e

This will open an editor window. To run the cli cron script every 15 minutes, add the line:

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

NOTE: the final >/dev/null sends all the output to the 'bin' and stops you getting an email every 15 minutes.

Setting up cron on your system

Choose the information for your server type:

Here are some more instructions for specific hosts (please check that these are up to date):

Using third party cron service

Besides using cron hosted on your own server, you may use third party cron service (usually called webcron):

  • EasyCron - A webcron service provider that eliminates the need of crontab or other task schedulers to set cron job.

Cron settings in Moodle

There are settings within Moodle that control aspects of cron operation:

Remote cron

Using the 'web based' version of cron it is perfectly ok to place the cron process on a different machine to the Moodle server. For example, the cron service on a Unix server can invoke the cron web 'page' on a Windows based Moodle server.

See also

Using Moodle forum discussions: