0

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:

  1. 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.
  2. Try assigning a name in the 'start' command I used, but that did not help.
  3. Try assigning a name in the first batch file with 'TITLE', but that did not help.

1 Answers1

0

This is vbs.

Set WshShell = WScript.CreateObject("WScript.Shell")
Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2") 
Set objEvents = objWMIService.ExecNotificationQuery _
    ("SELECT * FROM Win32_ProcessStopTrace")

Do
    Set objReceivedEvent = objEvents.NextEvent
    msgbox objReceivedEvent.ProcessName
    If lcase(objReceivedEvent.ProcessName) = lcase("Notepad.exe") then 
        Msgbox "Process exited with exit code " & objReceivedEvent.ExitStatus
        WshShell.Run "c:\Windows\notepad.exe", 1, false
    End If
Loop

What it does it waits for a program to exit (all programs) and if notepad (in the example) restarts it. You'd check for php.exe or whatever it's called.

Or use the following to get the command line and check by PID.

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")

Set colItems = objWMIService.ExecQuery("Select * From Win32_Process")

For Each objItem in colItems
    msgbox objItem.ProcessID & " " & objItem.CommandLine
Next 

Use Instr to check for your PHP filename in command line and store the PID.

This would also work

Set WshShell = WScript.CreateObject("WScript.Shell")
Do
    Return = WshShell.Run("notepad " & WScript.ScriptFullName, 0, true)
Loop

It starts notepad and waits for it to exit, then starts it again forever.

The same technique in batch

:ALabel
start "" /w notepad.exe
Goto ALabel 
  • i can check for "cmd.exe", but since there can also be other instances of "cmd.exe", I want to give the process a name and then check for this name. – Willem van Oort Jan 13 '16 at 10:18
  • How you want to, and how you can solve your problem are two different things. –  Jan 13 '16 at 11:06
  • I know, but I cannot solve my problem by looking for 'cmd.exe', because there can be multiple instances of 'cmd.exe' running. That is why I need to identify this instance by something else, for example a process name. I am open to other solutions of course. – Willem van Oort Jan 13 '16 at 17:52
  • That's what my answer does, find the PHP.exe and the one with your php file. CMD doesn't run your file, PHP does. –  Jan 13 '16 at 18:56
  • alright, but how do i know it is the PHP.exe instance that is running my specific file, because there can be multiple instances of PHP.exe as well. I have never used VBS so i might be missing something. – Willem van Oort Jan 13 '16 at 20:30
  • The second half of my answer addresses that. Do an instr on your command line looking for `YourFile.php`. Store the PID and set the ProcessStop up and check if it's that PID exiting. –  Jan 13 '16 at 20:39