How to add scheduled task to run php scripts in Windows

Few days ago, for one of our PHP / MySQL project, we were needed to run two php scripts at every minute. We usually develop/deploy our projects in LAMP environment, where we use crontab for our scheduled tasks. But our client have wanted us to run the system in Windows. So we have chosen  XAMPP. To run our two php scripts every 1 & 2 minute, we thought of developing a simple tool for windows which would run at background and execute those two php scripts. Fortunately we have found about schtasks, a native scheduler in Windows to run scheduled task smoothly in command prompt.

I will not describe our actual situation, rather I will demonstrate with 2 simple php scripts.

1. Create a directory in your htdocs folder inside XAMPP installation directory. In my case, it was C:\xampplite\htdocs\test

2. Create two php scripts, test1.php and test2.php inside test directory using the following respective code.
//creates, appends current time in a file called dump1.txt
$time = date(‘Y-m-d H:i:s’);
$file = "C:/xampplite/htdocs/test/dump1.txt";
file_put_contents($file,"job1 run on: $time\r\n", FILE_APPEND);
//creates, appends current time in a file called dump2.txt
$time = date(‘Y-m-d H:i:s’);
$file = "C:/xampplite/htdocs/test/dump2.txt";
file_put_contents($file,"job2 run on: $time\r\n", FILE_APPEND);

3. create a batch file php_service_creator.bat with the following code.
SCHTASKS /Create /RU SYSTEM /SC MINUTE /MO 1 /TN "test_sch_task1" /TR "C:\xampplite\php\php-win.exe -f C:\xampplite\htdocs\test\test1.php"
SCHTASKS /Create /RU SYSTEM /SC MINUTE /MO 5 /TN "test_sch_task2" /TR "C:\xampplite\php\php-win.exe -f C:\xampplite\htdocs\test\test2.php"

1. test_sch_task1 task will run on every minute.
2. test_sch_task2 task will run on every fifth minute.

to understand the used (and other possible) parameters, please visit this link

4. Run just created php_service_creator.bat. Please note that you have to run this from an account with administrative privileges.

5. After about 20 minutes, check C:/xampplite/htdocs/test/dump1.txt & C:/xampplite/htdocs/test/dump2.txt files for output.

6. To check/view all the added scheduled tasks, type schtasks in command prompt.

7. To delete any scheduled task, type schtasks /Delete /TN TASK_NAME_FOR_DELETE in command prompt.

I hope it helps some people like me.

Comments, suggestions are appreciated 🙂

  • hoàng anh

    thankyou very much:)

  • Tony

    Great job! Though I haven’t tried it yet, the sample is very well written, and much clearer than what I could find from Microsoft’s website.


  • Jochen

    Following your guidlines it doesn’t work for me! Using win7 64 pro.
    Having batchflie to create task:

    SCHTASKS /Create /RU SYSTEM /SC MINUTE /MO 1 /TN “test_sch_task1” /TR “C:xamppphpphp-win.exe -f Z:LaborhtdocsMematechworker.php”

    Task executes in win scheduler with returnvalue 1, fail! Allso nothing into log-file.

    Excecuting C:xamppphpphp-win.exe -f Z:LaborhtdocsMematechworker.php from cmd is working fine.
    The same behavier using php,exe.

    Any suggestions????

    Thanks in advance


  • Hi Jochen,

    May be in your case, you need to run the batch file with Administrative privileges. Right click on on the batch file and then select Run as Administrator. Please note that I might be wrong as I haven’t used a Windows machine for last 3 years 🙁

    Btw Try running the SCHTASKS command in cmd. Batch file is to simplify the task creation process.