I am implementing some web service written in php which executes a batch file. The batch code should be something like below,
for /f "usebackq tokens=2 delims=," %%i in (`tasklist /v /fo csv ^| findstr "cmd.exe" ^| findstr "THISFILE" ^| findstr /v "tasklist"`) do (
set killingpid=%%i
echo !killingpid!
set killing=!killingpid:~1,-1!
echo !killing!
taskkill /f /t /pid !killing!
)
title THISFILE
rem main program starts from here.
In this batch file, I am first trying to specify the process ID which has been executed by the very same batch file, if any. Then kill it. After that name the window title to specify the process. then start the main executions.
When The PHP script is executed by using "start" command, the new cmd window will pop up, and the program will be run perfectly.
$command = "start %FILEPATH%/batch.bat";
$process = popen($command, 'r');
But the problem is, When I try to execute the cmd.exe on the background by using "start /b", it won't work.
$command = "start /b %FILEPATH%/batch.bat";
$process = popen($command, 'r');
Since other cmd.exe programs might be running, I can not write something like
taskkill /f /fi "imagename eq cmd.exe"
How can I kill the specific process without naming the window title? Somebody please give me some instruction.