Part 9 - Setup a Moodle Cron Job

Cron jobs allow you to automate routine and repetitive tasks on the server such as sending out email messages and unsubscribing inactive members from classes. In Moodle this is done by the cron.php script. However there is no built-in way for this script to run every few minutes from Moodle, so you have to get some outside help.

One way of automating this task is to install the Moodle Cron Package. It simply installs a new Windows Service that will automatically run the cron.php script. However, if you have directory security enabled on your IIS Moodle installation - and you will need to if, for example, you want to use NTLM/LDAP authentication - don't use MoodleCron. A better way to run the cron.php script on Windows is to create a Scheduled Task in the Windows Scheduler on the server.

Another good reason for using a Scheduled Task on Windows is that if the Cron Script (cron.php) is called over HTTP (e.g. using WGET or CURL) it can use up a large amount of memory on large Moodle installations.

  1. Open the Windows Control Panel.
  2. Click the Scheduled Tasks option in the Control Panel.
  3. Double-click on Add Scheduled Task. The Scheduled Task Wizard will be displayed. Click the Next button.

  4. Click the Browse button.

  5. Browse to the PHP folder and select the php.exe file. Click the Open button.

  6. Enter Moodle Cron as the name of the task and select Daily as the schedule. Then click the Next button.

  7. Select 12:00 AM as the start time, perform the task Every Day and choose today's date as the starting date. Click Next.

  8. Enter the username and password of the user that the task will run under.

  9. Select the Open advanced properties for this task when I click Finish checkbox and click the Finish button.

  10. Instruct the cron job to run the php.exe program with "-f" and the "cron.php" file as parameters, i.e.:
    c:\php\php.exe -f c:\moodle\admin\cron.php.

    Note

    You can log the output of the cron script as it executes. This is useful if, for example, you see the job is generating errors, backups are not being completed or users are not receiving forum emails. To enable logging, adjust the command so that it uses php.exe and specify the name and path of the log file, example:
    C:\PHP\php.exe -f C:\moodle\admin\cron.php > C:\moodle\admin\cron.log

  11. Enter the name of the user that the cron job will run under in the Run as field.
  12. Click the Set password button and enter the relevant password for that user.

    Note

    You should run the scheduled task under a user account whose password does not expire. If the user password does expire then the scheduled task will not run when the password has expired and you might spend hours trying to find out why it is not running. One way to get around this is to create a new user in Windows specifically for running the scheduled task. The Password never expires option for this user must be selected.

  13. Open the Schedule tab and click the Advanced button.

  14. Select the Repeat task checkbox and set the Every field to 30 minutes (or any value that you want). If you select 30 minutes the cron.php script will be run every 30 minutes.
  15. Set the Until field to Duration.
  16. Enter 23 hours and 59 minutes as the duration. This means that the task will run continuously throughout each day and execute cron.php script at every interval that specified in section 14 above.
  17. Click the OK button to close the Advanced Schedule Options dialog box and again to close the Moodle Cron dialog box.
  18. The Moodle Cron job will be displayed in the Scheduled Tasks window and is ready to run in 30 minutes time (or whatever value you specified).

    Note

    To check if the task completed successfully look at the Last Result column. The 0x0 code indicates that the task completed successfully. Additionally if you click the View Log option on the Advanced menu in Windows Explorer the log file for the task will be displayed.

    If you find that the Moodle Cron script is not running properly it is most likely because that user that you are running the Scheduled Task under does not have the right permissions to execute the Moodle Cron script.

  19. Of course, you can still run the cron.php script manually at any time you want. To do this just enter the url to the cron.php file in your web browser, e.g. http://yoursite/moodle/admin/cron.php. You can run this file from any computer - it does not have to be from your Moodle web server.

The Moodle setup is now finished. The final step is to test the installation thoroughly and load a few users and some content. But first grab a drink and congratulate yourself!

Note

If you experience any problems setting up the cron job read the official Moodle Cron documentation and the excellent Q308558 article on the Microsoft Knowledge Base.


Back | Home
Contact