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

Cron: Difference between revisions

From MoodleDocs
 
(69 intermediate revisions by 32 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}}
Cron is the name of a Unix program that runs predefined tasks on a computer at regular intervals. The cron process in Moodle assists some modules to perform tasks on a scheduled basis. For example, the cron process might tell Moodle to check all discussion forums so it can mail out copies of new posts to people who have subscribed to that forum.  


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'''.
The Moodle cron process can not tell itself to run.  It is a best practice to set up a cron service either on the hosting web server, another server or on another computer that will tell the Moodle cron process to run.  
*[[Cron reports]] - Examples of reports shown after admin/cron.php was triggered
*[[Cron settings]] - Moodle cron process password and CLI settings
*[[Cron with Windows OS]] - Cron services in Windows
*[[Cron with MAC OS X]]- Cron services in a MAC environment
*[[Cron with web hosting services]]- Cron services in various web hosting examples.
*[[Cron with UNIX]]- Cron services on various UNIX and Linux flavored operating systems.


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.
:Note: Asking a human to use their browser to run the Moodle cron process every five or ten minutes,or when anybody on the site thinks it needs to be run is not a best practice.   The outside cron service provides a "heartbeat" so that the Moodle cron process can perform functions at periods defined for each module that needs 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.
==Starting cron==
There are a number of way to invoke Moodle cron process. Cron can be started from the address bar in a browser (For example http:demo.moodle.net/admin/cron.php), via a [[Daemon]], or wq1et, curl or some other form of a cron service.  


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.
===Cron service location and timing===
Note that the machine providing the cron service  '''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 Moodle cron process is run on a regular basis.


First, test that the script works by running it directly from your browser: ''<nowiki>http://example.com/moodle/admin/cron.php</nowiki>''
The load of the Moodle cron process on the Moodle server is not very high, so 5 minutes is usually reasonable.  However 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.  For example delaying mail-outs can slow down activity within the course and create a large mail outbox to process. Or student want to see their activity and course completions updated quickly.


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.
===Testing cron and manual trigger===
On a new Moodle install or upgrade, it is a good idea to test the Moodle cron process directly from your browser: ''<nowiki>http://example.com/moodle/admin/cron.php</nowiki>''  (See [[Cron settings]] if this does not work).


Now, you need to set up some of way of running the script automatically and regularly.
Next, you need to set up a way to manage an automatic scheduled process to run the script on a regular basis. This will depend upon the operating system and program you select.


==On Windows systems==
:Note: When the Moodle cron process is called from cron service, 'the command line' trigger creates a temporary admin environment (similar to a login) in order to run and then deletes that environment. You can disable command line running of cron by disabling the appropriate section in the cron.php file.


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! :-)
== Using third party cron service ==
Besides using cron hosted on your own server, you may use third party cron service (usually called webcron):


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:
*[https://www.easycron.com EasyCron] - A webcron service provider that eliminates the need of crontab or other task schedulers to set cron job.


# 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.
==Moodle cron process ==
# Copy the wget.exe file to c:\windows (this makes sure wget is always in the search path).
The Moodle cron process basically finds and determines if certain functions need to run. These functions are defined in code associated with specific activities and processes. Usually the function looks for new activity that has occurred since cron was last run. Some of the functions may use a timestamp to determine if they should look for new activity. A few functions are run on a random basis.
# 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.


You can test that your 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 executes and fetches the cron page and then it disappears.
===Examples of Moodle cron processes===
Moodle's cron processes include:
*updating reports such as quiz, admin, gradebook
*updating course and activity completion (if enabled in advanced settings)
*updating portfolio
*plagiarism checks
*updates activity modules. It looks through the mod directory for lib.php files that contain the function activity-name_cron and will call it. In a standard install this includes assignment, chat, forum, and SCROM.
*updates blocks.  It looks for blocks for their cron methods (object functions) to be run.  It then, for each such block, runs the cron method for a new object associated with that block (for more details read admin/cron.php). These files (the lib.php files and the files where the block classes are defined) can contain cleanup functions, email functions or anything that needs to be run on a regular basis.  
*create the backups of courses at the time specified in the administration settings.
*updating messaging module or forum email notifications.
*unenrol students - this is done on a random basis about 20% of the time Moodle's cron process is triggered.
*deleting users who have not filled out their profile via the 20% random trigger
*deleting old logs are also checked 20% of the time via the 20% random trigger
*deletes old cached text
*generates new passwords for new users and notifies users
*runs authentication enrolments processes
*updates stats if enabled.
*runs blog cleanups
*updates registrations


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.
The code in lib/cronlib.php shows the places that are being checked when the admin/cron.php is run and the report which is displayed on the screen after it has run. As mentioned elsewhere admin/cli/cron.php is the file which outside cron services run to trigger the Moodle cron process.
 
==On web hosting services==
 
Your web-based control panel may have a web page that allows you to set up this cron process.
 
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.
 
Email address for output: emailaddress@mydomain.con
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.
 
[[Image:CPanel-cron-setup.JPG]]
 
For other systems, look for a button called "Cron jobs". In there you can put the same sort of Unix commands as listed below.
 
==Using the command line on Unix==
 
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.
 
For example, you can use a Unix utility like 'wget':
 
wget -q -O /dev/null <nowiki>http://example.com/moodle/admin/cron.php</nowiki>
 
Note in this example that the output is thrown away (to /dev/null).
 
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 <nowiki>http://example.com/moodle/admin/cron.php</nowiki>
 
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>
 
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).


==See also==
==See also==
* [http://www.coursebit.net/moodle-cron-job/ All About the Moodle Cron Job]


*Using Moodle [http://moodle.org/mod/forum/discuss.php?d=41827 Cron - can someone give me a quick confirmation of function?] forum discussion
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]]
[[nl:Cron]]
[[sk:Cron]]
[[pl:Cron]]
[[ja:Cron]]

Latest revision as of 07:57, 9 March 2016

Cron is the name of a Unix program that runs predefined tasks on a computer at regular intervals. The cron process in Moodle assists some modules to perform tasks on a scheduled basis. For example, the cron process might tell Moodle to check all discussion forums so it can mail out copies of new posts to people who have subscribed to that forum.

The Moodle cron process can not tell itself to run. It is a best practice to set up a cron service either on the hosting web server, another server or on another computer that will tell the Moodle cron process to run.

Note: Asking a human to use their browser to run the Moodle cron process every five or ten minutes,or when anybody on the site thinks it needs to be run is not a best practice. The outside cron service provides a "heartbeat" so that the Moodle cron process can perform functions at periods defined for each module that needs it.

Starting cron

There are a number of way to invoke Moodle cron process. Cron can be started from the address bar in a browser (For example http:demo.moodle.net/admin/cron.php), via a Daemon, or wq1et, curl or some other form of a cron service.

Cron service location and timing

Note that the machine providing the cron service 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 Moodle cron process is run on a regular basis.

The load of the Moodle cron process on the Moodle server is not very high, so 5 minutes is usually reasonable. However 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. For example delaying mail-outs can slow down activity within the course and create a large mail outbox to process. Or student want to see their activity and course completions updated quickly.

Testing cron and manual trigger

On a new Moodle install or upgrade, it is a good idea to test the Moodle cron process directly from your browser: http://example.com/moodle/admin/cron.php (See Cron settings if this does not work).

Next, you need to set up a way to manage an automatic scheduled process to run the script on a regular basis. This will depend upon the operating system and program you select.

Note: When the Moodle cron process is called from cron service, 'the command line' trigger creates a temporary admin environment (similar to a login) in order to run and then deletes that environment. You can disable command line running of cron by disabling the appropriate section in the cron.php file.

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.

Moodle cron process

The Moodle cron process basically finds and determines if certain functions need to run. These functions are defined in code associated with specific activities and processes. Usually the function looks for new activity that has occurred since cron was last run. Some of the functions may use a timestamp to determine if they should look for new activity. A few functions are run on a random basis.

Examples of Moodle cron processes

Moodle's cron processes include:

  • updating reports such as quiz, admin, gradebook
  • updating course and activity completion (if enabled in advanced settings)
  • updating portfolio
  • plagiarism checks
  • updates activity modules. It looks through the mod directory for lib.php files that contain the function activity-name_cron and will call it. In a standard install this includes assignment, chat, forum, and SCROM.
  • updates blocks. It looks for blocks for their cron methods (object functions) to be run. It then, for each such block, runs the cron method for a new object associated with that block (for more details read admin/cron.php). These files (the lib.php files and the files where the block classes are defined) can contain cleanup functions, email functions or anything that needs to be run on a regular basis.
  • create the backups of courses at the time specified in the administration settings.
  • updating messaging module or forum email notifications.
  • unenrol students - this is done on a random basis about 20% of the time Moodle's cron process is triggered.
  • deleting users who have not filled out their profile via the 20% random trigger
  • deleting old logs are also checked 20% of the time via the 20% random trigger
  • deletes old cached text
  • generates new passwords for new users and notifies users
  • runs authentication enrolments processes
  • updates stats if enabled.
  • runs blog cleanups
  • updates registrations

The code in lib/cronlib.php shows the places that are being checked when the admin/cron.php is run and the report which is displayed on the screen after it has run. As mentioned elsewhere admin/cli/cron.php is the file which outside cron services run to trigger the Moodle cron process.

See also

Using Moodle forum discussions: