Note: You are currently viewing documentation for Moodle 3.8. Up-to-date documentation for the latest stable version of Moodle may be available here: Cron.

Cron: Difference between revisions

From MoodleDocs
m (typo)
No edit summary
 
(101 intermediate revisions by 41 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.'''


The cron.php script looks through all the module directories for cron.php files and runs them.  These files can contain cleanup functions, email functions or anything that needs to be run on a regular basis. For example, cron will trigger the system to create the backups of courses at the time specified in the administration settings. It also triggers any messaging module or forum email notifications, but not all functions are called each time the cron runs. Some functions, such as unenrolling students who have not logged in or deleting old copies of log files, are only run occasionally. The cron.php file has a section which will randomly call these core tasks approximately 1 in 5 times the cron runs.
It is recommended that ''the cron is run every minute'', as required for asynchronous activity deletion when using the [[Recycle bin|recycle bin]].


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 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.
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. Most shared hosting systems use CPanel to manage sites, and usually will have a section for Cron Jobs on the panel.


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. Remember that mail-outs also wait for the editing time to expire before being queued for sending.
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.


First, test that the script works by running it directly from your browser: ''<nowiki>http://example.com/moodle/admin/cron.php</nowiki>''
== General discussion ==


If cron is called from the command line by any user logged in to your Moodle it will create a temporary admin environment in order to run and then log the user out. You can disable command line running of cron by disabling the appropriate section in the cron.php file.
See the later sections for your server type; this section contains some general background information.  


Now, you need to set up some of way of running the script automatically and regularly.
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 Windows systems==
=== Working out the Moodle cron command ===


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! :-)
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 <pre>/path/to/moodle/admin/cli/cron.php</pre> 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 <pre>/usr/bin/php /path/to/moodle/admin/cli/cron.php</pre> You can (and should) try this on your command line to see if it works. '''WARNING: Check your command-line PHP version is compatible with your chosen version of Moodle. The command-line PHP program is different to the one running your web site and is not always the same version.'''
# If, for some reason, you cannot run the CLI script there is the web based script. Note that this is now deprecated and may be removed in future versions. 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 <pre>/usr/bin/wget http://your.moodle.site/admin/cron.php</pre> 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.


On the other hand, if you are having trouble with moodle-cron-for-windows package, you can use wget for windows and setup a scheduled task. Just follow these steps:
===The web based Moodle cron command===
* If you have a choice, do not use the web based cron. It is likely to be removed in a future Moodle version.
* From Moodle 2.9 onwards, the cron job can no longer be run from web by default. You will get an error message:
!!! Sorry, internet access to this page has been disabled by the administrator. !!!
* You can change this in ' Dashboard ► Site administration ► Security ► Site policies ' by deselecting 'Cron execution via command line only'.
** You will be warned that 'Running the cron from a web browser can expose privileged information to anonymous users. Thus it is recommended to only run the cron from the command line or set a cron password for remote access.'
** You can then write a 'Cron password for remote access'. If this field is left empty, no password is required.
** This means that the cron.php script cannot be run from a web browser without supplying the password using the following form of URL:
  http://site.example.com/admin/cron.php?password=opensesame


# Download a compiled version of wget for windows from Heiko Herold's wget for windows page (http://xoomer.virgilio.it/hherold/) or Bart Puype's wget for windows page (http://users.ugent.be/~bpuype/wget/). If you use Heiko Herold's package, copy all of the .DLL files to your C:\Windows\system32 directory.
=== Finding the right place to put the command ===
# Copy the wget.exe file to c:\windows (this makes sure wget is always in the search path).
# Go to Start >> Control Panel >> Scheduled Tasks >> Add Scheduled Task.
## Click "Next" to start the wizard:
## Click in the "Browse..." button and browse to C:\windows\wget.exe and click "Open"
## Type "Moodle Cron" as the name of the task and select "Daily" as the schedule. Click "Next".
## Select "12:00 AM" as the start time, perform the task "Every Day" and choose today's date as the starting date. Click "Next".
## Enter the username and password of the user the task will run under (it doesn't have to be a priviledged account at all). Make sure you type the password correctly :-). Click "Next".
## Mark the checkbox titled "Open advanced properties for this task when I click Finish" and click "Finish".
## In the new dialog box, type the following in the "Run:" text box:<pre><nowiki>
C:\windows\wget.exe -q -O NUL http://my.moodle.site/moodle/admin/cron.php
</nowiki></pre>
## Replace "my.moode.site" above with the name of your site.
## Click on the "Schedule" tab and there in the "Advanced..." button.
## Mark the "Repeat task" checkbox and set "Every:" to 5 minutes, and set "Until:" to "Duration" and type "23" hours and "59" minutes.
## Click "OK" and you are done.


A third option, if wget is also causing difficulties, is to use php.exe (command line binary). If you are using the windows installer package, you already have this. Follow the sceduled tasks instructions above, but in the 'run' box, enter
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.


php cron.php
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.  


and in the 'Start in:' box type the path to your moodle admin directory e.g.
Example... installing cron on Ubuntu/Debian Linux. Assuming logged in as root..


  C:\path-to-moodle\admin\
''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 1 minute, add the line:''
<pre>
* * * * * /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 1 minute.


== Setting up cron on your system ==
Choose the information for your server type:


You can test that your scheduled task can run successfully by clicking it with the right button and chosing "Run". If everything is correctly setup, you will briefly see a DOS command window while wget/php executes and fetches the cron page and then it disappears.
*[[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.


If you refresh the scheduled tasks folder, you will see the Last Run Time column (in detailed folder view) reflects the current time, and that the Last Result column displays "0x0" (everything went OK). If either of these is different, then you should recheck your setup.
Here are some more instructions for specific hosts (please check that these are up to date):


==On web hosting services==
*[[Cron on 1and1 shared servers]]


Your web-based control panel may have a web page that allows you to set up this cron process.
== Using third party cron service ==
Besides using cron hosted on your own server, you may use third party cron service (usually called webcron):


If you are using CPanel, login then look for the heading "Advanced" on the page. Click on Cron Jobs -> Advanced (unix style). Enter the following for the cron to run every 30 minutes.
*[https://cron-job.org/ cron-job.org] is a free service. (1Minute cron is possible)


Email address for output: emailaddress@mydomain.con
*[https://www.easycron.com EasyCron] - A webcron service provider that eliminates the need of crontab or other task schedulers to set cron job.
Minute:*/30
Hour:*
Day:*
Month:*
Weekday:*
Command: wget -q -O /dev/null http://www.mydomain.com/moodle/admin/cron.php


Click Commit Changes. Check your email for the output. An example is shown below:
*[https://webcron.talent-factory.ch/ WebCron] - A free and easy webcron service provider.


[[Image:Cpanel-cron-setup.JPG]]
=== Cron settings in Moodle ===


For other systems, look for a button called "Cron jobs". In there you can put the same sort of Unix commands as listed below.
An admin can set cron execution via command line only or a cron password for remote access in 'Site security settings' in the Site administration.


==Using the command line on Unix==
===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.


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.
==Scheduling tasks==
An administrator can schedule cron tasks very precisely from Administration > Site administration > Server > Scheduled tasks, see [[Scheduled tasks]]


For example, you can use a Unix utility like 'wget':
==Running cron for several Moodle servers==
* Tasks can run in parallel and processes use locking to prevent tasks from running at the same time which allows cron to be triggered from multiple web servers that serve the same Moodle instance.


wget -q -O /dev/null <nowiki>http://example.com/moodle/admin/cron.php</nowiki>
* If you are running different Moodle instances on the same server, then each Moodle instance needs a cron job. (Even a single Apache web server can run different Moodle instances on different domains by using its virtual hosts capability [https://httpd.apache.org/docs/2.2/vhosts/index.html https://httpd.apache.org/docs/2.2/vhosts/index.html].)


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


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:
Sometimes, a particular cron task may not be working correctly. In Moodle versions before 2.7 - any cron task that was throwing exceptions would prevent the rest of cron from running. The only way to monitor if cron was completing each time, was to add some automated checking of the output of running cron (e.g. searching for the string "Cron completed at ").


php <nowiki>http://example.com/moodle/admin/cron.php</nowiki>
In Moodle 2.7 and later, a single failing scheduled task will not prevent the remaining tasks from completing. When any single scheduled task fails, it is marked as a failure, and scheduled to be reattempted. If the task keeps failing, the next scheduled time will be backed off until it is attempted at most once every 24 hours. But checking the [[Scheduled tasks]] admin page, you can see if any task is currently failing (it will have a non-zero fail delay - which is the number of seconds to wait before reattempting a failed task). A simple way to debug a failing task, is to run it immediately using the [[Administration via command line#Scheduled_tasks|cli scheduled task runner]] and monitor the output.
 
The same thing using lynx:
 
lynx -dump <nowiki>http://example.com/moodle/admin/cron.php</nowiki> > /dev/null
 
Note in this example that the output is thrown away (to /dev/null).
 
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.
 
/opt/bin/php /web/moodle/admin/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:
 
*/5 * * * * wget -q -O /dev/null <nowiki>http://example.com/moodle/admin/cron.php</nowiki>
 
The first five entries are the times to run values, followed by the command to run. The asterisk is a wildard, indicating any time. The above example means run the command ''wget -q -O /dev/null...'' every 5 minutes (*/5), every hour (*), every day of the month (*), every month (*), every day of the week (*). 
 
* [http://linuxweblog.com/node/24 A basic crontab tutorial]
* [http://www.freebsd.org/cgi/man.cgi?query=crontab&apropos=0&sektion=5&manpath=FreeBSD+6.0-RELEASE+and+Ports&format=html Online version of the man page]
 
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 [http://www.unix-manuals.com/tutorials/vi/vi-in-10-1.html intro] to the 'vi' editor.


==See also==
==See also==


*Using Moodle [http://moodle.org/mod/forum/discuss.php?d=41827 Cron - can someone give me a quick confirmation of function?] forum discussion
* [[Scheduled tasks]]
* [http://en.wikipedia.org/wiki/Cron Wikipedia article on cron function]
* MDL-50694 - Cron message "The operation timed out while waiting for a lock" isn't really an error


[[Category:Core]]
Forum discussions:
[[Category:Administrator]]
*[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.
[[Category:Installation]]


[[es:Cron]]
[[es:Cron]]
[[fr:Cron]]
[[fr:Cron]]
[[nl:Cron]]
[[ja:Cron]]
[[de:Cron-Job]]

Latest revision as of 08:33, 13 April 2020

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.

It is recommended that the cron is run every minute, as required for asynchronous activity deletion when using the recycle bin.

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. Most shared hosting systems use CPanel to manage sites, and usually will have a section for Cron Jobs on the panel.

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. WARNING: Check your command-line PHP version is compatible with your chosen version of Moodle. The command-line PHP program is different to the one running your web site and is not always the same version.
  2. If, for some reason, you cannot run the CLI script there is the web based script. Note that this is now deprecated and may be removed in future versions. 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.

The web based Moodle cron command

  • If you have a choice, do not use the web based cron. It is likely to be removed in a future Moodle version.
  • From Moodle 2.9 onwards, the cron job can no longer be run from web by default. You will get an error message:
!!! Sorry, internet access to this page has been disabled by the administrator. !!! 
  • You can change this in ' Dashboard ► Site administration ► Security ► Site policies ' by deselecting 'Cron execution via command line only'.
    • You will be warned that 'Running the cron from a web browser can expose privileged information to anonymous users. Thus it is recommended to only run the cron from the command line or set a cron password for remote access.'
    • You can then write a 'Cron password for remote access'. If this field is left empty, no password is required.
    • This means that the cron.php script cannot be run from a web browser without supplying the password using the following form of URL:
 http://site.example.com/admin/cron.php?password=opensesame

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 1 minute, add the line:

* * * * * /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 1 minute.

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.
  • WebCron - A free and easy webcron service provider.

Cron settings in Moodle

An admin can set cron execution via command line only or a cron password for remote access in 'Site security settings' in the Site administration.

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.

Scheduling tasks

An administrator can schedule cron tasks very precisely from Administration > Site administration > Server > Scheduled tasks, see Scheduled tasks

Running cron for several Moodle servers

  • Tasks can run in parallel and processes use locking to prevent tasks from running at the same time which allows cron to be triggered from multiple web servers that serve the same Moodle instance.
  • If you are running different Moodle instances on the same server, then each Moodle instance needs a cron job. (Even a single Apache web server can run different Moodle instances on different domains by using its virtual hosts capability https://httpd.apache.org/docs/2.2/vhosts/index.html.)

Debugging Scheduled Tasks

Sometimes, a particular cron task may not be working correctly. In Moodle versions before 2.7 - any cron task that was throwing exceptions would prevent the rest of cron from running. The only way to monitor if cron was completing each time, was to add some automated checking of the output of running cron (e.g. searching for the string "Cron completed at ").

In Moodle 2.7 and later, a single failing scheduled task will not prevent the remaining tasks from completing. When any single scheduled task fails, it is marked as a failure, and scheduled to be reattempted. If the task keeps failing, the next scheduled time will be backed off until it is attempted at most once every 24 hours. But checking the Scheduled tasks admin page, you can see if any task is currently failing (it will have a non-zero fail delay - which is the number of seconds to wait before reattempting a failed task). A simple way to debug a failing task, is to run it immediately using the cli scheduled task runner and monitor the output.

See also

Forum discussions: