Cron with Windows OS

Jump to: navigation, search

There are two different ways for creating a Moodle Cron process trigger on Windows operating systems.

Moodle cron package

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

wget or php 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
or
c:\php\php-win.exe -f c:\moodle\admin\cli\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.

NOTE: If you use the wget version, be sure to check Cron settings to make sure that the 'web based' cron service is permitted.

  • 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

If you experience problems logging the output of cron.php to a text file using the above command then read this message by Iñaki Arenaza for an alternative way to log the output of Cron.

Another method is to create a small batch file on your server that does all the work and get the scheduled task to call that file. Here is an example of a batch file that saves the output of cron to a file and deletes log files older than 5 days.

@echo off
set phppath="C:\Program Files (x86)\PHP\v5.3\php.exe"
set cronpath="C:\moodle\moodle-test\wwwroot\admin\cli\cron.php"
set logpath="D:\moodle\moodle-test\cronlogs"

%phppath% -f %cronpath% >> %logpath%\%date:~10,4%%date:~4,2%%date:~7,2%.log

Forfiles /P %logpath% /S /M *.log /D -5 /C "cmd /c del /q @path"