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

纵饮孤独 提交于 2019-12-17 18:26:36

问题


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

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


回答1:


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.

Using 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:

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



回答2:


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.

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):

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



回答3:


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):

@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



回答4:


you can try with

wmic process call create "notepad"

which will return the pid of the created process.




回答5:


Thanks a lot to user - npocmaka.

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

:: 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

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%



来源:https://stackoverflow.com/questions/9486960/how-to-get-pid-of-process-just-started-from-within-a-batch-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!