How to capture the PID of a process when launching

2020-01-27 07:53发布

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.

标签: process cmd pid
8条回答
我想做一个坏孩纸
2楼-- · 2020-01-27 08:05

Since the highest rated answer so far uses another, built-in tool of Windows, here is another take, based on a temporary JScript script executed via WSH (Windows Scripting Host), which is also included in Windows.

One advantage to calling wmic.exe is that this does not rely on the output of wmic.exe, should it ever change (not likely, but anyway):

@echo off

if "%~1" equ "" echo Example usage in cmd.exe:
if "%~1" equ "" echo.
if "%~1" equ "" echo  startandgetpid.bat notepad.exe "1 2.txt"
if "%~1" equ "" echo  echo New process' ID: %%errorlevel%%
if "%~1" equ "" exit /b -1

:: Temporary file name for JScript script:
set fn=%temp%\startandgetpid%random%.js

:: Create JScript which launches a new process
:: and returns the process ID of the new process
:: via its exit code:
echo s = "", args = WScript.Arguments; for (i = 0; i ^< args.length; i++) { s += '"' + args(i) + '" '; } WScript.Quit(WScript.CreateObject("WScript.Shell").Exec(s).ProcessId);>"%fn%"

:: Execute the JScript script:
"%fn%" %*

:: Fetch the exit code, which is the process ID:
set pid=%errorlevel%

:: Delete the temporary JScript script:
del "%fn%"

:: Explicitly exit the execution of this batch
:: script with the PID as exit code:
exit /b %pid%
查看更多
老娘就宠你
3楼-- · 2020-01-27 08:13

Here's what I use:

@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 /I %%i EQU ProcessId (
        set pid=%%j
    )
)

echo %pid% > MyProg.pid

The directory is set to the directory that the cmd file is located in. Change dir to alter that. Modify cmd to change which command is run.

If you want a stop.cmd that kills it, it would look like this

@echo off
for /F %%i in (%~dsp0MyProg.pid) do taskkill /F /PID %%i
del %~dsp0MyProg.pid
查看更多
爷、活的狠高调
4楼-- · 2020-01-27 08:23

I think you can't do that with simple command line utilities, as IE actually spawns child processes for each tab, i.e. if IE is not yet running you would get one parent IE process and a child process for the tab, and if IE is already running you would simply get a single child process.

It will be even quite tricky when you write your own tool to kill IE because when you kill a child (tab) process, IE will automatically recover this tab.

See also this related question: How to obtain process of newly created IE8 window? (though there is no good answer there).

查看更多
Evening l夕情丶
5楼-- · 2020-01-27 08:24

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
查看更多
三岁会撩人
6楼-- · 2020-01-27 08:25
放荡不羁爱自由
7楼-- · 2020-01-27 08:29

Most often you do know what task you start - in this case, which page iexplorer shall show.

So how about

taskkill /FI "Windowtitle eq *yourpagetitle*"

It will kill all instances of something showing your page title, but with a specific title most often there should be exactly one.

Tom

查看更多
登录 后发表回答