Getting PID from running bat script
Hi I'm trying to find a way to get my own PID from a bat script. I found this:
title=mycmd
tasklist /v /fo csv | findstr /i "mycmd"
which outputs:
"cmd.exe","15084","RDP-Tcp#5","2","2,768 K","Running","MEDIASERVER\Administrator
","0:00:00","Administrator: =mycmd"
how do i get the PID number in a variable in my bat script?
any help would be appreciated.
+3
source to share
2 answers
try getcmdpid so you don't need to change the title:
call getCmdPID.bat
echo %errorlevel%
to do it with a todo list that you need for a loop to process the output:
title mycmd
for /f "tokens=2 delims=," %%a in (
'tasklist /v /fo csv ^| findstr /i "mycmd"'
) do (
set "mypid=%%~a"
)
echo %mypid%
check also this thread: http://www.dostips.com/forum/viewtopic.php?t=6133
+3
source to share
@echo off setlocal enableextensions disabledelayedexpansion rem Prepare a temporary file reference where to send the wmic output for %%t in ("%temp%\%~nx0.%random%%random%%random%%random%%random%.tmp") do > "%%~ft" ( rem Call wmic to retrieve its own parent process ID, that is, our cmd instance wmic process where "name='wmic.exe' and commandline like '%%_%random%%random%%random%_%%'" get parentprocessid rem Read the PID from the generated temporary file for /f "skip=1" %%a in ('type "%%~ft"') do set "processID=%%a" rem And remove the temporary file ) & 2>nul del /q "%%~ft" echo %processID%
+4
source to share