How to capture the PID of a process when launching it from command line?

前端 未结 8 1914
栀梦
栀梦 2020-11-30 13:48

Is there a way to do this purely in a .bat file?

The purpose is to launch iexplore.exe, then kill just that instance when it\'s finished.

8条回答
  •  失恋的感觉
    2020-11-30 14:39

    A slight variation on the answer provided by @kybernetikos since it has a parsing issue. Note the line if %%j gr 0 (

    @echo off
    
    rem there is a tab in the file at the end of the line below
    set tab=    
    set cmd=javaw -jar lib\MyProg.jar
    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
        )
    )
    
    echo %pid% > MyProg.pid
    

提交回复
热议问题