Cron
Alguns mòduls del Moodle necessiten comprovar si han de tirar endavant les tasques. Per exemple, el Moodle necessita comprovar els fòrums per si cal enviar còpies dels missatges enviats per la gent que hi ha subscrita.
L'escript que fa aquesta feina és al directori d'administració, i s'anomena cron.php. De totes maneres, no pot arrencar sol, i cal establir el mecanisme perquè l'escript treballi regularment (per exemple, cada cinc o deu minuts). Així se li proporciona una mena de "marcapassos" que farà que l'escript faci aquesta feina en períodes definits per cada mòdul. Aquesta mena de mecanisme es coneix com a servei cron.
L'escript cron.php revisa tots els directoris de mòduls cercant fitxers cron.php i els executa. Aquests fitxers poden contenir funcions de neteja, de correu electrònic o qualsevol feina que calgui per treballar de forma regular. Per exemple, el cron activa el sistema per fer les còpies de seguretat de cursos el dia i hora especificats als paràmetres d'administració. També activa els mòduls de missatgeria o els de les notificacion s de missatges als fòrums, però quan el cron s'activa no s'executen totes les accions. Algunes funcions, com ara les d'anul·lació d'inscripcions per desús, o l'eliminació de còpies antigues de fitxers de registre, que treballen més rarament. El fitxer cron.php té una secció que executarà aquestes tasques fonamentals aproximadament una vegada de cada cinc que el cron funcioni.
Penseu que la màquina que farà treballar el cron no cal que sigui la mateixa que la que té el Moodle. Per exemple, si teniu un servei web hostatjat limitat que no us permet tenir un servei cron, aleshores haureu de fer córrer el cron en un altre servidor o al vostre ordinador. Es tracta que el cron.php hagi d'actuar regularment.
La càrrega d'aquest escript no ha de ser gaire alta, i 5 minunts marquen un ritme normalment raonable, però si us ha de portar problemes, podeu reduir el període a 15 o 30 minuts. És millor no allargar gaire el període, perquè demorar la sortida de correu pot alentir l'activitat dins dels cursos. Recordeu que la sortida del correu també espera que s'acabi el temps marcat d'edició d'un missatge per enviar-lo.
Primerament, comproveu que l'escript treballa correctament fent-li una crida directa des del navegador: http://exemple.com/moodle/admin/cron.php
Si el cron el crida des de la línia de comandes qualsevol usuari identificat al Moodle, crearà un entorn temporal d'administració, a fi de fer córrer i més tard tornar a l'entorn de l'usuari. Podreu inhabilitat l'execució des de la línia de comandes desactivant la secció corresponent al fitxer cron.php.
Ara cal preparar-lo perquè l'escript s'executi automàticament i regular.
En sistemes Windows
Hi ha dues maneres de configurar el cron.php del Moodle en sistemes Windows:
- Utilitzar el Paquet Cron del Moodle. La manera més senzilla çonsisteix a utilitzar aquest paquet
moodle-cron-for-windows.zip que resol l'assumpte amb la instal·lació d'un servei Windows. Carregueu-lo i no us en torneu a preocupar! :-)
- Utilitzeu una Tasca programada. Si, d'altra banda, teniu algun problema amb el paquet cron-moodle-per_a-windows, podeu fer servir wget per al Windows o el php des de la línia de comandes i preparar una tasca programada. Podeu seguir aquests passos:
You can either use the php.exe (command line binary) which is installed in your php folder (e.g. c:\php), or you can use wget. Note that you will get better performance by using the php.exe option. If you want to use wget, 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. 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.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.
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. An example is shown below:
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 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 wildard, 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 (*).
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.
See also
- Using Moodle Cron - can someone give me a quick confirmation of function? forum discussion