如何在DOS中启动进程时捕获进程的PID [英] How to capture the PID of a process when launching it in DOS

查看:669
本文介绍了如何在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

这篇关于如何在DOS中启动进程时捕获进程的PID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆