如何在DOS中启动进程时捕获进程的PID
问题描述:
有没有办法纯粹在.bat文件?
目的是启动iexplore,然后在完成后杀死那个实例。
Is there a way to do this purely in a .bat file? The purpose is to launch iexplore, then kill just that instance when it's finished..
答
:
@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
目录设置为cmd文件所在的目录。更改 dir
来改变。修改 cmd
以更改运行的命令。
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.
如果你想要一个stop.cmd杀死它,看起来像这样
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