Running a Cron Job on Windows
Working with a Windows based web server recently, I ran into a bit of a puzzle when a software package I was using required a regular PHP script to be run. On POSIX based operating systems like Linux or BSD, this is something you could easily accomplish by running what is known as a ‘cronjob’.
As Windows has no cron daemon, I had to come up with another solution.
My research led me to the Windows Task Scheduler for the solution.
Open up the Windows Task Scheduler using the ‘control schedtasks’ command in a Run dialog.
The Task Scheduler is a lot more advanced than a simple cronjob, allowing you selective execution based on various criteria, and much much more. Below I will walk through the process of setting up a simple re-occuring PHP script execution.
To begin creating a task, look to the menu down the right hand side. Click on the ‘Create Task’ menu item.
The Create Task window will pop open, and you will be on the General tab. Enter a name and description for your new task.
Now click on the Trigger tab. In the Windows Task Scheduler world, you can set up a task to be run based on any number of events, not just time like with a cronjob. To create a ‘Trigger’, click on the New button.
The New Trigger window will open up. To have our task run every 10 minutes as an exmaple, look at the settings in the screenshot above. Begin the task: On a schedule. Run the task One time. Repeat task every 10 minutes for a duration of Indefinitely.
Be sure to mark the trigger as ‘Enabled’ by clicking the checkbox at the bottom of the window before selecting OK.
Now with the Trigger set up, we need to define an Action. It may seem obvious, but to be clear, the Action is what will actually happen, in our case, the execution of a PHP script.
Click on the Actions tab, then the ‘New’ button to open up the New Action dialog.
Here you need to set the Action to Start a program. Point the Program/script dialog to your php.exe, and put the name of the script you want to run as an argument.
Look at the image above as an example.
With the Trigger and Action now in place, we can safely save our new Task. Before doing so however you may want to have a look at the Conditions and Settings tabs for more in depth options. If you’re coming from the POSIX world like me, you may be amazed by all the options you have over these tasks!
Once your task is saved, and you are back on the main Task Scheduler window, you can confirm it was created successfully by looking at the Active Tasks dialog.
The name of your task should appear, along with the Next run time.






Just as an extra note here, I realized after creating this task that every time it would run, a command window popped up into the foreground. This got rather tiresome every ten minutes, especially as I was busy doing something else!
Try and get around this, I created a runphp.bat file, which contained the following:
start /min C:\PHP\php.exe C:\path\to\myscript.php
Then I modified the Action on my task to run this batch file (with no arguments). The effect of doing this is that when the task runs, it will start in a minimized window!
fdask
20 Jun 09 at 12:23 pm edit_comment_link(__('Edit', 'sandbox'), ' ', ''); ?>