找出特定实例的nunit的进程ID

时间:2018-11-26 11:31:23

标签: batch-file nunit taskkill

我有三个批处理文件,每个文件打开一个NUnit实例。测试完成后,我想杀死NUnit的特定实例。我能够获取三个NUnit实例的三个进程ID。

但是有没有办法找出哪个processId对应于哪个Nunit实例?

检查下面的图像。

enter image description here

此处显示了进程ID。但是无法识别哪个实例是哪个实例。

这是我的代码,用于杀死NUnit进程ID。

for /F "tokens=1,2,*" %%a in ('tasklist /FI "IMAGENAME eq nunit.exe"') do (
SETLOCAL ENABLEDELAYEDEXPANSION
set PID=%%b
echo PID StartRun_XXX=!PID!)

::echo before loop 
:: Check for TestResult.xml file generation. 
:: at the moment when TestResult.xml generated, kill the NUNIT instance and subsequently call OverViewGenerator.bat to generate TestResult.htm


pause
:loop
if exist %~dp0TestResult_%TestCategory%_%TimeStamp%.xml (
    cd %~dp0
    echo ====================inside if exist------dp0=%~dp0===================
    pause
    echo PID=!PID!
    taskkill /PID !PID! /T

此代码会突然杀死任何nunit实例。

1 个答案:

答案 0 :(得分:1)

您最好的选择可能是在启动nunit流程时加以区分。

可以获取通过WMIC启动的进程的PID:

set "tab="  <<<<<<<<<<<< PLEASE insert TAB after '=' 
set "pid="
for /F "usebackq tokens=1,2 delims=;=%tab% " %%i in (           
     `wmic process call create "nunit.exe"^, "C:\working\dir"`
) do if %%j gtr 0 set pid=%%j

(从this答案中复制)

然后您可以将PID保存到文件中

echo set "PID=%pid%" >yourfile.bat

要终止进程,只需运行脚本以获取变量

call yourfile.bat
taskkill /pid %pid%
taskkill /f /pid %pid%
del /y yourfile.bat

另请参阅:How to get the output of "wmic process call create"

相关问题