2

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.

agongji
  • 117
  • 1
  • 7
  • 2
    `start /b` causes the started process to share the same console (and so it's title) with it's motherprocess, the input and the output alternately processed by the one or the other. While you probably can distinguish which output comes from which process, you will never know for sure, which process tries to take your input right now. So I'm not sure if this is a good approach (Ymmv). But anyway: the process started with `start /b` has the Window Title `N/A` (language dependent!) - in spite of the `title` command, which sets the title of the *console* (which is still owned by the motherprocess!) – Stephan Nov 22 '20 at 09:19
  • Stephan, thank you for your comment. When ‘start /b’ command is run via web server, there is no console to share. As you pointed out, window title is gonna be ‘N/A’. ( it can be considered as a kind of ‘background process’, can’t it ). Anyway I don’t wish any consoles to pop up when this task is done. – agongji Nov 22 '20 at 10:21
  • 2
    how about replacing `findstr "THISFILE"` with `findstr "N/A"`? (btw: `%%~i` removes the quotes, so just `taskkill /f /t /pid:%%~i` should do the job.) – Stephan Nov 22 '20 at 10:27

1 Answers1

0

I finally used a power-shell command. something like below.

set PIDFile=%PATH_TO%\pid.txt

rem if the same file has been running, then kill it.
if exist "%PIDFile%" (
    set /p targetPID=<%PIDFile%
    taskkill /f /t /pid !targetPID!
    del !PIDFile!
)

rem write this process ID down on the file so that other processces can read it.
powershell "Get-WmiObject win32_process -filter processid=$pid | ForEach-Object{$_.parentprocessid;}" >> %PATH_TO%\pid.txt

rem main process here

del !PIDfile!
agongji
  • 117
  • 1
  • 7