从命令行启动进程时如何捕获进程的PID? [英] How to capture the PID of a process when launching it from command line?

查看:50
本文介绍了从命令行启动进程时如何捕获进程的PID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法完全在 .bat 文件中做到这一点?

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

目的是启动 iexplore.exe,然后在它完成时杀死那个实例.

The purpose is to launch iexplore.exe, 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 libMyProg.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

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

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