0

I have three batch files, each opening one instance of NUnit. I want to kill a particular instance of NUnit when the tests are completed. I am able to fetch the three process ID's for three NUnit instances.

But is there a way to find out which processId corresponds to which Nunit instance?

Check the image below.

enter image description here

Here it is showing me process ID. But cannot recognize which one is for which instance.

Here is my code in batch for killing the NUnit process 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

This code abruptly kills any nunit instance.

freedomn-m
  • 27,664
  • 8
  • 35
  • 57
Karthik Saxena
  • 768
  • 3
  • 9
  • 25
  • The for loop will overwrite the PID with all found nunit PIDs, so only the last one persists. In the if code block taskkill will only kill this last one PID. –  Nov 26 '18 at 11:57
  • Why not check the PID's before and after each instance? _then you can determine which PID belongs to which respective instance before deciding which to kill_. – Compo Nov 26 '18 at 13:58

1 Answers1

1

Your best bet is probably distinguishing nunit processes when they are launched.

It is possible to obtain PID of a process started through WMIC:

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

(copied from this answer)

Then you can save PID into a file

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

To kill the process just run the script to obtain the variable

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

See also: How to get the output of "wmic process call create"

Jack White
  • 896
  • 5
  • 7