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

Cron with Unix or Linux: Difference between revisions

From MoodleDocs
m (added link to spanish translation of page)
No edit summary
 
(4 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{Cron}}
{{Installing Moodle}}
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.  
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.  


Line 17: Line 17:


You can simply type this on the command line this to see if it works. If you are not sure about the path to PHP you can type "<code>which php</code>".
You can simply type this on the command line this to see if it works. If you are not sure about the path to PHP you can type "<code>which php</code>".
'''NOTE:''' This Moodle file 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.


'''Tip:''': If you have problems, see the [[PHP]] page. In particular, suspect an alternate php.ini for the CLI PHP command which may not have suitable settings.
'''Tip:''': If you have problems, see the [[PHP]] page. In particular, suspect an alternate php.ini for the CLI PHP command which may not have suitable settings.
Line 46: Line 44:
==Using the crontab program on Unix/Linux==
==Using the crontab program on Unix/Linux==


Once you have selected (and tested!) an appropriate command to invoke the Moodle cron it must be added to the web users 'crontab' to schedule it to run regularly. 'Crontab' is both a file containing the user's cron commands and is also the name of the (command line) program used to edit it. Use the following command (as root) substituting the correct user in place of 'www-data' (e.g. 'apache' for Centos, 'www-data' for Debian/Ubuntu - Google will know!)
Once you have selected (and tested!) an appropriate command to invoke the Moodle cron it must be added to the web users 'crontab' to schedule it to run regularly. 'Crontab' is both a file containing the user's cron commands and is also the name of the (command line) program used to edit it. Use the following command (as root) substituting the correct user in place of 'www-data' (e.g. 'apache' for Centos, 'www-data' for Debian/Ubuntu, '_www' for macOS—Google will know!)
<pre>
<pre>
# crontab -u www-data -e
# crontab -u www-data -e
Line 52: Line 50:
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 in this way (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 in this way (it may be empty or it may have some instructional comments):
<pre>
<pre>
  */15 * * * *       /usr/bin/php /path/to/moodle/admin/cli/cron.php
  */1 * * * * /usr/bin/php /path/to/moodle/admin/cli/cron.php >/dev/null
</pre>
The first five entries specify the times, followed by the command, to run. This says to run the command every minute which is normally ok. 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). If you want to use the wget/curl version, the first five entries remain the same - just change the command part.
 
== High performance cron tasks ==
 
Each time cron is run, after the scheduled tasks the adhoc tasks are also run. But adhoc tasks can be queued at any moment, and generally you want them processed as soon as possible and to not have to wait for the scheduled task to run first. You can run one or more dedicated adhoc task processors:
 
<pre>
* * * * * /usr/bin/php  /path/to/moodle/admin/cli/adhoc_task.php --execute >/dev/null
</pre>
</pre>
The first five entries specify the times, 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). If you want to use the wget/curl version, the first five entries remain the same - just change the command part.
 
Starting from Moodle 3.9 you can tell it to stay alive when the queue is empty waiting for new tasks:
 
<pre>
* * * * * /usr/bin/php  /path/to/moodle/admin/cli/adhoc_task.php --execute --keep-alive=59 >/dev/null
</pre>
 
If you have a very high low number of adhoc tasks, or scheduled tasks, you can also spawn multiple cron.php and adhoc_task.php processes to get more throughput. By default Moodle will make sure than no more than 3 processes run at any time, you can tune these limits as needed in  Site administration > Server > Tasks > Task processing


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

Latest revision as of 08:14, 14 August 2020

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.

Method 1: The command line (cli) cron

If you have a choice, this is normally the best way to run Moodle cron.

PHP is also capable of running programs directly from the command line. Your system needs to be set up to do this; specifically you need the 'CLI' version of PHP to be installed. Most systems with PHP installed will have this by default. If you have the PHP CLI version installed then this is the recommended method of invoking cron. The correct command will be something like...

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

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

You can simply type this on the command line this to see if it works. If you are not sure about the path to PHP you can type "which php".

Tip:: If you have problems, see the PHP page. In particular, suspect an alternate php.ini for the CLI PHP command which may not have suitable settings.

Method 2: 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 permitted.

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 on the server. Possibilities are as follows (OSX, for example, only ships with curl)...

/usr/bin/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...

/usr/bin/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 (and tested!) an appropriate command to invoke the Moodle cron it must be added to the web users 'crontab' to schedule it to run regularly. 'Crontab' is both a file containing the user's cron commands and is also the name of the (command line) program used to edit it. Use the following command (as root) substituting the correct user in place of 'www-data' (e.g. 'apache' for Centos, 'www-data' for Debian/Ubuntu, '_www' for macOS—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 in this way (it may be empty or it may have some instructional comments):

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

The first five entries specify the times, followed by the command, to run. This says to run the command every minute which is normally ok. 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). If you want to use the wget/curl version, the first five entries remain the same - just change the command part.

High performance cron tasks

Each time cron is run, after the scheduled tasks the adhoc tasks are also run. But adhoc tasks can be queued at any moment, and generally you want them processed as soon as possible and to not have to wait for the scheduled task to run first. You can run one or more dedicated adhoc task processors:

 * * * * * /usr/bin/php  /path/to/moodle/admin/cli/adhoc_task.php --execute >/dev/null

Starting from Moodle 3.9 you can tell it to stay alive when the queue is empty waiting for new tasks:

 * * * * * /usr/bin/php  /path/to/moodle/admin/cli/adhoc_task.php --execute --keep-alive=59 >/dev/null

If you have a very high low number of adhoc tasks, or scheduled tasks, you can also spawn multiple cron.php and adhoc_task.php processes to get more throughput. By default Moodle will make sure than no more than 3 processes run at any time, you can tune these limits as needed in Site administration > Server > Tasks > Task processing

See also