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 with Unix or Linux.

Cron with Unix or Linux: Difference between revisions

From MoodleDocs
(copying recent changes from 21 wiki)
Line 1: Line 1:
{{Cron}}{{Update}}
{{Cron}}
There are different command line programs you can use to call the [[Cron]] page from the command line. Not all of them may be available on a given server.
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.  


'''Note:''' The examples with wget, lynx, and similar are '''not''' the same as the "CLI only" cron checkbox, mentioned above (the configuration variable "cronclionly"). wget, lynx, and other similar utilities are Unix command-line HTTP clients, and thus running cron.php in this way is the same as running it in a browser, from Moodle's point of view.
There are two different methods that can be used to invoke the Moodle cron process:


==wget==
'''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.
For example, you can use a Unix utility like 'wget':


wget -q -O /dev/null <nowiki>http://example.com/moodle/admin/cron.php</nowiki>
== The command line (cli) cron ==


Note in this example that the output is thrown away (to /dev/null).
If you have the PHP CLI version installed then this is the recommended method of invoking cron. The correct command is as follows...
<pre>
/usr/bin/php /path/to/moodle/admin/cli/cron.php
</pre>
(substitute the correct path to moodle and for php as required)


==php==
'''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.
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>
'''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.


The same thing using lynx:
== Web based cron ==


lynx -dump <nowiki>http://example.com/moodle/admin/cron.php</nowiki> > /dev/null
'''NOTE:''' In order to use the web based cron script you must first check [[Cron settings]] to make sure this method is permitted.  


Note in this example that the output is thrown away (to /dev/null).
The idea is to call the following web page (you can try this from your browser):
<pre>
http://url.of.your/moodle/admin/cron.php
</pre>


Alternatively, you can use a standalone version of PHP, compiled to be run on the command line. The disadvantage is that you need to have access to a command-line version of php. The advantage is that your web server logs aren't filled with constant requests to cron.php and you can run at a lower I/O and CPU priority.
A command line (text based) browser is needed to run this from the server. Possibilities are as follows (OSX, for example, only ships with curl)...
<pre>
/usr/bin/wget -q -O /dev/null/ http://url.of.your/moodle/admin/cron.php
</pre>
(no output is displayed - remove the ''-O /dev/null/'' to test)


/opt/bin/php /web/moodle/admin/cli/cron.php
...OR...


Example command to run at lower priority:
<pre>
/usr/bin/curl http://url.of.your/moodle/admin/cron.php -o /dev/null/ -silent
</pre>
(no output is displayed - remove the ''-o /dev/null/ -silent'' to test)


  ionice -c3 -p$$;nice -n 10 /usr/bin/php /moodle/admin/cli/cron.php > /dev/null
==Using the crontab program on Unix/Linux==


'''Note: In version 2.0 and later the path for running cron.php from the commandline has changed. Use c:\moodle\admin\cli\cron.php'''
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. 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!)
<pre>
# crontab -u www-data -e
</pre>
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>
  */15 * * * *        /usr/bin/php /path/to/moodle/admin/cli/cron.php
</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.


==Using the crontab program on Unix==
== See also ==
 
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:
 
*/30 * * * * 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 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 "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]  
* [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]  
* [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]
 
For '''beginners''', "EDITOR=nano crontab -e" will allow you to edit the crontab using the [http://www.nano-editor.org/dist/v1.2/faq.html 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 [http://www.unix-manuals.com/tutorials/vi/vi-in-10-1.html intro] to the 'vi' editor.

Revision as of 14:59, 8 December 2011

Template:Cron 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.

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.

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 from 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. 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!)

# 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):

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

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.

See also