Install php scripts as cron in Windows


Linux has it’s very unique and beautiful feature “cron” that allows us to execute a program at a predefined schedule. For LAMP developers cron is a very common feature that we use very frequently. But many time lots of developers who recently have hosted their php code in Windows servers, face difficulty setting up their scripts to be run on a scheduled time as they did via LINUX cron. So here is a simple way of doing it in Vista:

Lets say we have a php script named “mycron.php” that needs to be run everyday at 12:00pm (this timing can be of many types like run every 5 minutes, every 1 hour etc.). Windows has a tool called “Task scheduler” similar to LINUX cron feature. You can find it from

Control Panel >> Administrative Tools >> Task Scheduler

To run a php script in windows automatically, first we need to create a batch file. Let’s call it mycron.bat. Batch files are simple text files with the extension .bat. It contains the commands that need to be run. Lets say PHP is installed at C:\php folder and we have saved our php script at C:\crons. We need to add the following line to run our php script from the batch file:

start C:\php\php.exe C:\crons\mycron.php

Save the batch file(lets save it in C:\crons\mycron.bat) and it is ready to be scheduled. If we want more than one script to be run, we can add them in newlines or create a separate batch file for each php script. For example a batch file with multiple entry may look like this:

start C:\php\php.exe C:\crons\mycron.php
start C:\php\php.exe C:\crons\mycron1.php
start C:\php\php.exe C:\crons\mycron2.php

Now that our batch file is ready, we need to add it to our Task Scheduler. We can find the task scheduler at Control Panel >> Administrative Tools >> Task Scheduler (in Vista)

  • Find the Create Task link on the right panel of the window. Click it.
  • A new window will pop out with different tabs like General, Triggers, Action, Condition, Settings.
  • On the “General” tab, Enter the Name and some description (optional) of the task. We can change the user or group here. If the task need to do some actions that need administrative privilege, we should make sure our script is run from an Administrator account.
  • Go to “Triggers” tab and click “New”. Another window will pop up with lots of options of when and how many times we want to run our script. For our case we’ll select “Daily” and on the time portion of the “Start” field, select 12:00 pm. We don’t need to change any other options for our particular purpose, but anyone can try different combination as they need it. Click “OK” and we’ll find an entry in the “Triggers” tab.
  • Now we need to go to the “Actions” tab where we’ll set up our batch file. Click “New” and a window will pop up. In the “Action” dropdown, we’ll select Start Program. In the “Program/script” field we’ll select our batch file “mycron.php”.
    • There is an “Add arguments” field which is very useful when we want to run our script via a web browser. I have added the link to task scheduler’s usage detail at the end of the article where you may find how to use it.
  • Click “OK” and we’ll find an entry in the “Actions” tab.
  • In the “Conditios” tab we can add some conditions to run the script. All the options are self explanatory so I don’t think I need to explain.
  • In the “Settings” tab there are some more options of scheduling the script. Read the Task Scheduler usage detail at the following link for more detail:

http://technet.microsoft.com/en-us/library/cc721871(WS.10).aspx

We can now click “OK” and our task is scheduled and ready to be run. Finally, in the main “Task Scheduler” window, select the task we have just created and click “Start” from the right panel of the window and we are done. Our script should be running everyday at 12:00 pm.

, , ,

  1. #1 by sam on February 3, 2012 - 6:09 pm

    Thanx for this detailed explanation. I have been all about Linux Cron for all my life. Knowing i could also automate on windows for PHP apps really makes me happy.

  2. #2 by sany on May 24, 2012 - 6:45 am

    Thanks for your detailed explanation. keep it up …..thanks again

  3. #3 by adhi on August 31, 2012 - 11:48 am

    thanx for info.. helpfully.. thanks

  4. #4 by cron job on May 5, 2013 - 9:02 am

    Another handy way is to use webcron.

  5. #5 by John on November 14, 2013 - 6:55 am

    thank you for your nice write-up. It was exectly what I was looking for. Works like a charme.

  6. #6 by Georgi on February 20, 2014 - 3:47 pm

    Hello, great tuturial.
    I done everything as you write, but my php script actually didn`t run. When I started throgh the WAMP – it worked and write in the file, but when I run in through task schedule like .bat file – nothing happen.My .bat file contains :
    start C:\wamp\bin\php\php5.4.12\php.exe C:\wamp\www\printToFile.php
    And in task scheduler for Program/Script field I point to place where this was saved.
    Can you help me why this is not working ?
    Thanks you in advance.
    Regards, Georgi

(will not be published)


eight − 7 =