windows 如何从批处理文件中获取刚刚启动的进程的PID?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9486960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 09:17:05  来源:igfitidea点击:

How to get PID of process just started from within a batch file?

windowsscriptingbatch-file

提问by Sergey V. Pereslavtsev

In Windows batch scripting there is startcommand which starts a new process.

在 Windows 批处理脚本中有start启动新进程的命令。

Is it possible to get PID of the process just started?

是否可以获取刚刚启动的进程的 PID?

回答by zappee

This is an old post but I think that it worth to share the following 'easy to use' solution which works fine nowadays on Windows.

这是一篇旧帖子,但我认为值得分享以下“易于使用”的解决方案,该解决方案如今在 Windows 上运行良好。

Start multiple processes in parallel:

并行启动多个进程:

start "<window title>" <command will be executed>

Example:

例子:

start "service1" mvn clean spring-boot:run
start "service2" mvn clean spring-boot:run

Obtain the PID of the processes (optional):

获取进程的PID(可选):

tasklist /V /FI "WindowTitle eq service1*"
tasklist /V /FI "WindowTitle eq service2*"

Kill the processes:

杀死进程:

taskkill /FI "WindowTitle eq service1*" /T /F
taskkill /FI "WindowTitle eq service2*" /T /F

回答by Andy Arismendi

You can in batch but not directly per say. You need to either parse the output of tasklist.exe or use wmic.exe. Both require you to know what you just started which of course you will.

你可以批量但不能直接说。您需要解析 tasklist.exe 的输出或使用 wmic.exe。两者都要求您知道您刚刚开始的内容,当然您会知道。

Using tasklist.exe:

使用 tasklist.exe:

for /F "TOKENS=1,2,*" %a in ('tasklist /FI "IMAGENAME eq powershell.exe"') do set MyPID=%b
echo %MyPID%

To use this in a batch script double up the percent signs.

要在批处理脚本中使用它,请将百分号加倍。

Using wmic.exe:

使用 wmic.exe:

for /f "TOKENS=1" %a in ('wmic PROCESS where "Name='powershell.exe'" get ProcessID ^| findstr [0-9]') do set MyPID=%a
echo  %MyPID%

回答by Oliver Zendel

If there are processes already running with the same name, you first need to get a list of the current pids, than start your local process(es) and then check the pids again. Here is a sample code that starts 3 process and kills them at the end (specifically the ones started locally):

如果已经有同名进程在运行,您首先需要获取当前 pid 的列表,然后启动本地进程,然后再次检查 pid。这是一个示例代码,它启动 3 个进程并在最后杀死它们(特别是在本地启动的那些):

@echo off
set PROCESSNAME=notepad.exe

::First save current pids with the wanted process name
setlocal EnableExtensions EnableDelayedExpansion
set "RETPIDS="
set "OLDPIDS=p"
for /f "TOKENS=1" %%a in ('wmic PROCESS where "Name='%PROCESSNAME%'" get ProcessID ^| findstr [0-9]') do (set "OLDPIDS=!OLDPIDS!%%ap")

::Spawn new process(es)
start %PROCESSNAME%
start %PROCESSNAME%
start %PROCESSNAME%

::Check and find processes missing in the old pid list
for /f "TOKENS=1" %%a in ('wmic PROCESS where "Name='%PROCESSNAME%'" get ProcessID ^| findstr [0-9]') do (
if "!OLDPIDS:p%%ap=zz!"=="%OLDPIDS%" (set "RETPIDS=/PID %%a !RETPIDS!")
)

::Kill the new threads (but no other)
taskkill %RETPIDS% /T > NUL 2>&1
endlocal

回答by npocmaka

you can try with

你可以试试

wmic process call create "notepad"

which will return the pid of the created process.

这将返回创建的进程的 pid。

回答by frog.ca

PowerShell can be used for this:

PowerShell 可用于:

powershell -executionPolicy bypass -command "& {$process = start-process $args[0] -passthru -argumentlist $args[1..($args.length-1)]; exit $process.id}" notepad test.txt

echo Process ID of new process: %errorlevel%

回答by German Geraskin

Thanks a lot to user - npocmaka.

非常感谢用户 - npocmaka。

There are examples of how to start Mozilla Firefox and get PID of the started process.

有一些示例说明如何启动 Mozilla Firefox 并获取已启动进程的 PID。

:: Example for cmd window

set AppCmdLine="C:\Program Files (x86)\Mozilla Firefox\firefox.exe -new-window http://www.google.com/"
set ProcessCmd=wmic process call create %AppCmdLine%
for /f "tokens=3 delims=; " %a in ('%ProcessCmd% ^| find "ProcessId"') do set PID=%a

echo %PID%

The same example for bat script

bat脚本的相同示例

set AppCmdLine="C:\Program Files (x86)\Mozilla Firefox\firefox.exe -new-window http://www.google.com/"
set ProcessCmd=wmic process call create %AppCmdLine%
for /f "tokens=3 delims=; " %%a in ('%ProcessCmd% ^| find "ProcessId"') do set PID=%%a
echo %PID%