I am running a webapplication on Windows Server 2012 and have to run an endless loop in a PHP file. In order to control this, I created two batch files.
The PHP file:
- <?php
- while(true) {
- sleep(10);
- }
- ?>
BatchFile that calls the PHP file:
- TITLE WatchdogStarterBATCH
-
- php "%~dp0watchdog.php"
- timeout 5
Batchfile 2
- @ECHO OFF
-
- :: Detect whether program is running
- for /f "tokens=2 delims=," %%P in ('tasklist /v /fo csv ^| findstr /i "WatchdogStarterBATCH"') do set pid=%%~P
-
- IF [%pid%] == [] (
- :: Program not running, trying to restart
- start "WatchdogStarterBATCH" "%~dp0WatchdogStarter.bat"
- timeout 5
- GOTO rerun
- ) ELSE (
- :: Program is running
- GOTO end
- )
-
- :rerun
- :: Check whether program is running now
- for /f "tokens=2 delims=," %%P in ('tasklist /v /fo csv ^| findstr /i "WatchdogStarterBATCH"') do set pid=%%~P
-
- IF [%pid%] == [] (
- :: Restart failed, log to database
- php "%~dp0WatchdogErrorLogger.php"
- ) ELSE (
- :: Restart successful, log to database
- php "%~dp0WatchdogWarningLogger.php"
- GOTO end
- )
-
- :end
- echo Done.
- timeout 5
Batchfile 2 is called by the Task Scheduler and then calles WatchdogStarter.bat as you can see. It tries to give this service a name, so that it can find it later in the tasklist.
Things I tried:
- Run batfile2 from command line and check if i can find it the string "WatchdogStarterBATCH" in the tasklist. I was able to find it, so that works.
- Try assigning a name in the 'start' command I used, but that did not help.
- Try assigning a name in the first batch file with 'TITLE', but that did not help.