Cron: Difference between revisions
Érika Santos (talk | contribs) No edit summary |
Érika Santos (talk | contribs) No edit summary |
||
Line 3: | Line 3: | ||
O script primário do Moodle que faz tudo isso está localizado no diretório admin, e é chamado de cron.php. Contudo, ele não pode se auto-executar, então você precisa estabelecer um mecanismo no qual o script é executado regularmente (ex. a cada 5 ou 10 minutos). Isso fornece um "ritmo" para que o script possa realizar funções em períodos definidos por cada módulo. Esse tipo de mecanismo regular é conhecido como '''serviço cron'''. O serviço pode ser parte de um webhost ou pode ser algo executado a partir de um servidor ou computador diferente. | O script primário do Moodle que faz tudo isso está localizado no diretório admin, e é chamado de cron.php. Contudo, ele não pode se auto-executar, então você precisa estabelecer um mecanismo no qual o script é executado regularmente (ex. a cada 5 ou 10 minutos). Isso fornece um "ritmo" para que o script possa realizar funções em períodos definidos por cada módulo. Esse tipo de mecanismo regular é conhecido como '''serviço cron'''. O serviço pode ser parte de um webhost ou pode ser algo executado a partir de um servidor ou computador diferente. | ||
== | ==Sobre o cron== | ||
=== | ===Sobre o script=== | ||
O script cron.php procura através da tabela mdl_modules (assumindo que a tabela o prefixo da tabela padrão seja mdl_) na base de dados do Moodle por módulos que tenham agendado funções do cron; então ele procura em cada diretório desses módulos por uma função chamada nome-do-modulo_cron no arquivo lib.php e a executa. Ele também procura através da tabela mdl_block por blocos que tenham agendado seus métodos cron (funções de objeto); então, para cada um desses blocos, ele executa o método cron para um novo objeto associado àquele bloco (eu estou omitindo detalhes para não complicar muito para não-programadores; os programadores podem ler admin/cron.php para mais informações). Esses arquivos (os lib.php e os nos quais as classes de bloco são definidas) podem conter funções de limpeza, email ou qualquer coisa que precise ser executada regularmente. 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. | O script cron.php procura através da tabela mdl_modules (assumindo que a tabela o prefixo da tabela padrão seja mdl_) na base de dados do Moodle por módulos que tenham agendado funções do cron; então ele procura em cada diretório desses módulos por uma função chamada nome-do-modulo_cron no arquivo lib.php e a executa. Ele também procura através da tabela mdl_block por blocos que tenham agendado seus métodos cron (funções de objeto); então, para cada um desses blocos, ele executa o método cron para um novo objeto associado àquele bloco (eu estou omitindo detalhes para não complicar muito para não-programadores; os programadores podem ler admin/cron.php para mais informações). Esses arquivos (os lib.php e os nos quais as classes de bloco são definidas) podem conter funções de limpeza, email ou qualquer coisa que precise ser executada regularmente. 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. | ||
Line 130: | Line 130: | ||
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. | 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. | ||
== | ==Ver também== | ||
Using Moodle forum discussions: | Using Moodle forum discussions: |
Revision as of 10:58, 7 August 2008
O cron auxilia alguns módulos do Moodle a executar tarefas de forma agendada. Por exemplo, o processo cron pode dizer ao Moodle para verificar todos os fóruns de discussão para que ele possa enviar cópias por email dos novos posts para os assinantes daquele fórum.
O script primário do Moodle que faz tudo isso está localizado no diretório admin, e é chamado de cron.php. Contudo, ele não pode se auto-executar, então você precisa estabelecer um mecanismo no qual o script é executado regularmente (ex. a cada 5 ou 10 minutos). Isso fornece um "ritmo" para que o script possa realizar funções em períodos definidos por cada módulo. Esse tipo de mecanismo regular é conhecido como serviço cron. O serviço pode ser parte de um webhost ou pode ser algo executado a partir de um servidor ou computador diferente.
Sobre o cron
Sobre o script
O script cron.php procura através da tabela mdl_modules (assumindo que a tabela o prefixo da tabela padrão seja mdl_) na base de dados do Moodle por módulos que tenham agendado funções do cron; então ele procura em cada diretório desses módulos por uma função chamada nome-do-modulo_cron no arquivo lib.php e a executa. Ele também procura através da tabela mdl_block por blocos que tenham agendado seus métodos cron (funções de objeto); então, para cada um desses blocos, ele executa o método cron para um novo objeto associado àquele bloco (eu estou omitindo detalhes para não complicar muito para não-programadores; os programadores podem ler admin/cron.php para mais informações). Esses arquivos (os lib.php e os nos quais as classes de bloco são definidas) podem conter funções de limpeza, email ou qualquer coisa que precise ser executada regularmente. 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.
Cron service location and timing
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 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.
Testing cron and manual trigger
First, test that the script works by running it directly from your browser: http://example.com/moodle/admin/cron.php
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.
Now, you need to set up some of way of running the script automatically and regularly.
Managing Cron on Windows systems
There are two different ways for setting-up Moodle cron.php on Windows systems:
- Use the Moodle Cron package. The simplest way is to use this little package MoodleCron-Setup.exe, which makes this whole thing very easy by installing a small Windows service. Run it and forget about it! :-)
- Use a Scheduled Task. If you prefer to use the built-in Windows Scheduler or are having trouble with moodle-cron-for-windows package, you can use wget for windows or php from the command line and setup a scheduled task. Just follow these steps:
- Choose either the php.exe/php-win.exe (command line binary) or wget
- The php.exe or php-win.exe binary (for PHP version 5 or later) is installed in your php folder (e.g. c:\php) will give you better performance when running the cron script.
- If you want to use wget, download a compiled version of wget for windows from the native GNU Win32 ports (http://unxutils.sourceforge.net/), 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. Copy the wget.exe file to c:\windows (this makes sure wget is always in the search path).
- Setup a Scheduled Task.
- - 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:\php\php.exe or 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:
c:\windows\wget.exe -q -O NUL http://my.moodle.site/moodle/admin/cron.php
orc:\php\php-win.exe -f c:\moodle\admin\cron.php
Replace "c:\moodle" with the path to your moodle directory or "my.moode.site" 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.
- Test your scheduled task. 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. 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.
- Logging cron output. You may want to log the output of the cron script as it executes, in case you see the job is producing errors, backups are not being completed or users are experiencing delays in receiving forum emails. To do this, adjust the command so that it uses the php.exe and stores the output in a file called (for example c:\moodle\admin\cron.log). Here is an example of the php.exe command:
c:\php\php.exe -f c:\moodle\admin\cron.php > c:\moodle\admin\cron.log
Managing cron on web hosting services
Your web-based control panel may have a web page that allows you to set up a cron service process.
CPanel cron service
If you are using CPanel, login then look for "Advanced" category towards the bottom of 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.
Other systems cron service
For other systems, look for a button called "Cron jobs". In there you can put the same sort of Unix commands as listed below.
If you don't have permissions to run the 'wget' command on the server, you can use this php command:
/usr/local/bin/php -q /real/path/to/script/admin/cron.php
For example:
/usr/local/bin/php -q /home/username/public_html/moodle/admin/cron.php
If you don't know what is the real path of your Moodle folder you can use the PHP command realpath.
Another alternative, if you do not have permission to run the 'wget' command, may be to use a curl command.
For example:
curl --silent --compressed http://mydomain.com/moodle/admin/cron.php
Using a cron command line in 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 http://example.com/moodle/admin/cron.php
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 http://example.com/moodle/admin/cron.php
The same thing using lynx:
lynx -dump http://example.com/moodle/admin/cron.php > /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:
*/30 * * * * wget -q -O /dev/null http://example.com/moodle/admin/cron.php
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.
For beginners, "EDITOR=nano crontab -e" will allow you to edit the crontab using the 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 intro to the 'vi' editor.
Ver também
Using Moodle forum discussions: