在Windows批处理脚本中,有start
命令可以启动一个新进程。
是否有可能刚刚启动过程的PID?
答案 0 :(得分:17)
你可以批量但不是直接说。您需要解析tasklist.exe的输出或使用wmic.exe。两者都要求你知道你刚开始做什么,当然你会。
使用tasklist.exe:
for /F "TOKENS=1,2,*" %a in ('tasklist /FI "IMAGENAME eq powershell.exe"') do set MyPID=%b
echo %MyPID%
要在批处理脚本中使用它,请将百分号加倍。
使用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%
答案 1 :(得分:12)
如果正在运行的进程具有相同的名称,则首先需要获取当前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
答案 2 :(得分:11)
这是一篇旧文章,但我认为值得分享以下在Windows上现在可以正常使用的“易于使用”解决方案。
并行启动多个进程:
start "<window title>" <command will be executed>
示例:
start "service1" mvn clean spring-boot:run
start "service2" mvn clean spring-boot:run
获取进程的PID(可选):
tasklist /V /FI "WindowTitle eq service1*"
tasklist /V /FI "WindowTitle eq service2*"
终止进程:
taskkill /FI "WindowTitle eq service1*" /T /F
taskkill /FI "WindowTitle eq service2*" /T /F
答案 3 :(得分:1)
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%
答案 4 :(得分:0)
you can try with
wmic process call create "notepad"
which will return the pid of the created process.
答案 5 :(得分:0)
非常感谢用户-npocmaka。
有一些示例,介绍了如何启动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%
蝙蝠脚本的相同示例
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%
答案 6 :(得分:0)
这是我用来获取PID的代码
for /f "tokens=2 delims=," %%a in ('tasklist /FO CSV ^| findstr /I /C:"entertheprocess.here"') do (
echo PID:%%a
)
答案 7 :(得分:-1)
@ECHO OFF
SETLOCAL EnableDelayedExpansion EnableExtensions
::
::weil es mehrere Sessions für UltraCompare gleichzeitig geben kann, es wird hier die
::neueste Instanz (soeben gestartet) ermittelt, um später mit ProcessId diese Instanz
::aktivieren zu können...
::
set "CreationDate="
set /A "ProcessIdUltraCompare=0"
::
set /A "lineno=0"
FOR /F %%T IN ('Wmic process where^(Name^="uc.exe"^) get CreationDate^|sort /r') DO (
set /A lineno+=1
rem echo %%T
rem echo !lineno!
if !lineno! equ 2 (
rem ECHO %%T
set CreationDate=%%T
rem echo !CreationDate!
set /A "lineno=0"
FOR /F %%P IN ('Wmic process where^(CreationDate^="!CreationDate!"^) get ProcessId') DO (
set /A lineno+=1
rem echo %%P
if !lineno! equ 2 (
set "ProcessIdUltraCompare=%%P"
rem echo ProcessIdUltraCompare=!ProcessIdUltraCompare!
goto :l_pid_uc_got
)
)
)
)
:l_pid_uc_got
echo ProcessIdUltraCompare=!ProcessIdUltraCompare!
PAUSE