2

I am trying to run a program and get its PID so that I could terminate the program if it hung up during execution. I could not kill the program with program name since there will be more than one program running on the PC. I come across forums and got the suggestion of wmic usage but I could not get the output of the program with calling it with wmic.

It is about the same problem as mentioned in this post: Remote Netstat to STDOUT with WMIC?

I tried this command as suggested to execute the program and output:

   wmic process call create "cmd /C > C:\temp\test.txt 2>&1 netstat.exe -ano" 

With this command, I am able to retrieve the output of the program but then the program is actually called from another cmd? This defied the original purpose why I had initially trying to use wmic.

Anyone could tell if I have another way to do get the output or another way to call a program from batch, get its PID and the output redirection of the program called?

Community
  • 1
  • 1
yun
  • 35
  • 1
  • 4
  • `wmic Process Call Create ...` outputs the related `ProcessId`, so you could use this later by another `wmic` command line using a `Where ProcessId=...`, like `wmic Process Where ProcessId=... Call Terminate`, or by `taskkill /PID ...`... – aschipfl Nov 01 '16 at 22:35

2 Answers2

1

since you only need the PID to kill the process, you could also use the windowtitle:

start "UniqueWindowTitle" cmd /c "ping /t google.de >out.txt 2>&1"
timeout /t 10
taskkill /fi "WINDOWTITLE eq UniqueWindowTitle" 2>nul
type out.txt

You could also add /min as start switch to not show the box.

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • Thank you!! This looks like what I really need now. assigning uniqueWindowTitle to the program and kill it after that. Thanks!! :) – yun Nov 01 '16 at 09:10
  • lol... it looks like nothing wrong with both script actually. It is only that /F option for taskkill causing all the problem. The child program called in the cmd or wmic killed successfully without "/F". Or with "/F /T" will work too. Thank you all! :) – yun Nov 01 '16 at 13:38
  • yeah, the simple `taskkill`(without `/f`) is like "would you stop please?", but `/f` tells `taskkill` to just use it's gun and shoot the process down without warning. – Stephan Nov 01 '16 at 14:41
0

This an example from this question : How to capture the PID of a process when launching it in DOS ?

@echo off
rem there is a tab in the file at the end of the line below
set tab=    
set "cmd=cmd /k ^> C:\temp\test.txt 2^>^&1 netstat.exe -ano"
set dir=%~dp0

echo Starting MyProg
set pid=notfound

for /F "usebackq tokens=1,2 delims=;=%tab% " %%i in (
    `wmic process call create "%cmd%"^, "%dir%"`
) do (
    if %%j gtr 0 (
        set "pid=%%j"
    )
)
Call :Trim "%pid%"
echo MyPID = "%pid%"
echo Hit any key to kill the process created by WMIC 
pause>nul
Taskkill /F /PID %pid%
pause & exit
::*************************************************************************************
:Trim <String>
set "vbsfile=%tmp%\%~n0.vbs"
(
    echo Wscript.echo Trim("%~1"^)
)>"%vbsfile%"
for /f "delims=" %%a in ('Cscript /nologo "%vbsfile%"') do ( set "pid=%%a" )
If Exist "%vbsfile%" Del "%vbsfile%"
exit /b
::*************************************************************************************
Community
  • 1
  • 1
Hackoo
  • 18,337
  • 3
  • 40
  • 70
  • Hi Hackoo, Thank you for replying. This doesn't looks like what I need. Probably I had given a too easy example of program I wanted to run. The program is something like putty.exe (not exactly putty but same behavior) where cmd is calling the intend program on a new window and not execute within the cmd itself. With the script given above, PID appear here "echo MyPID = "%pid%"" is PID of the cmd rather than the intend program. When wmic kill the PID, it is only closing down the cmd window rather than the intend program. Hope that I explained it clear enough this time. – yun Nov 01 '16 at 08:57