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.
[php]<?php
//test1.php
//creates, appends current time in a file called dump1.txt
date_default_timezone_set(‘Asia/Dacca’);
$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);
?>[/php]
[php]<?php
//test2.php
//creates, appends current time in a file called dump2.txt
date_default_timezone_set(‘Asia/Dacca’);
$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);
?>[/php]

3. create a batch file php_service_creator.bat with the following code.
[vb]
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"
[/vb]

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

Tagged with:
 

Looking for something?

Use the form below to search the site:


Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...