Asked by:
What is the PID of the current cmd.exe

Question
-
During the build process I need to launch multiple batch files. However I need to track them by their PID (Process ID).
Is there an easy way of getting the current PID?
I could parse tlist for cmd.exe but that would return the 1st occurence.
Thanks!
Wednesday, March 14, 2007 9:30 PM
All replies
-
Hi, how do you launch the batch files? using the Exec task? and you need to track the PID of that batch process?Thursday, March 15, 2007 9:05 PM
-
All batch files are launched from main batch file that is executed from Visual Studio as part of the build/rebuild process.
The main batch file launches a varisble number of child batch files using start /C "cb.cmd"
So, at any moment I may have about 20 instances cb.cmd running.
What I have tried so far is to pass an instance number as an argument to cb.cmd.
Like :
for /L %%i IN (1,1,%AMDtargets%) do start cmd /C "echo Instance:%%i && cb.cmd %%i && pause"
(Note: Pause is usually not used)
But this does not work well when building has stopped and you need to rebuild again. It is not unique.
PID on the other hand is unique. Always.
Something to get the PID of the current cmd.exe
Set WhatPIDamI=GetPID
for /L %%i IN (1,1,%AMDtargets%) do start cmd /C "echo %WhatPIDamI% && pause"
So I need to the PID of the current cmd.exe. How can I get it?
Thanks
Thursday, December 6, 2007 5:53 PM -
A way that I got the PID of the current process is: (in batch file)
TITLE test
FOR /F "tokens=2 delims= " %%A IN ('TASKLIST /FI ^"WINDOWTITLE eq test^" /NH') DO SET my_pid=%%A
A little late, but hopefully it'll help someone!- Proposed as answer by thegoods Tuesday, August 25, 2009 8:06 PM
Tuesday, August 25, 2009 8:06 PM -
I came across a similar need to determine the Process ID of the currently executing script, but in a way that is multiple execution safe especially when the script can be executing multiple times at the exact same time in the same millisecond, by multiple users, in multiple window sessions (RDP), by privileged and non-privileged (e.g. Administrator) accounts, interactively or in the background.
Because the simple %PID% variable is not available in Batch this is a difficult task to do natively without resolving to third-party utilities or Win32 function calls. Below is code that I wrote that works
GetPID.cmd
@ echo off
rem Note: Session Name for privileged Administrative consoles is sometimes blank.
if not defined SESSIONNAME set SESSIONNAME=Console
setlocal
rem Instance Set
set instance=%DATE% %TIME% %RANDOM%
echo Instance: "%instance%"
title %instance%
rem PID Find
for /f "usebackq tokens=2" %%a in (`tasklist /FO list /FI "SESSIONNAME eq %SESSIONNAME%" /FI "USERNAME eq %USERNAME%" /FI "WINDOWTITLE eq %instance%" ^| find /i "PID:"`) do set PID=%%a
if not defined PID for /f "usebackq tokens=2" %%a in (`tasklist /FO list /FI "SESSIONNAME eq %SESSIONNAME%" /FI "USERNAME eq %USERNAME%" /FI "WINDOWTITLE eq Administrator: %instance%" ^| find /i "PID:"`) do set PID=%%a
if not defined PID echo !Error: Could not determine the Process ID of the current script. Exiting.& exit /b 1
rem Current Task Show
echo PID: "%PID%"
tasklist /v /FO list /FI "PID eq %PID%"
rem Title Reset to Image Name (Image Name can contain spaces and will not be tokenized through usage of * token and %%b variable to access the remaining line without tokenization.)
for /f "usebackq tokens=2*" %%a in (`tasklist /V /FO list /FI "PID eq %PID%" ^| find /i "Image Name:"`) do title %%bOutput
Instance: "Tue 07/10/2012 13:35:28.67 28676" PID: "15204" Image Name: cmd.exe PID: 15204 Session Name: Console Session#: 1 Mem Usage: 3,612 K Status: Running User Name: DOMAIN\username CPU Time: 0:00:00 Window Title: Tue 07/10/2012 13:35:28.67 28676
Tuesday, July 10, 2012 7:07 PM -
Nice. Thanks for the info
Thursday, August 23, 2012 6:27 AM -
The former TITLE-based solutions don't work when the cmd.exe window cannot appear (e.g. running from TaskScheduler as LOCAL SERVICE or using the "Run whether user is logged on or not" setting).
Here is a solution that works in these cases, too:
set T=%TEMP%\sthUnique.tmp
wmic process where (Name="WMIC.exe" AND CommandLine LIKE "%%%TIME%%%") get ParentProcessId /value | find "ParentProcessId" >%T%
set /P A=<%T%
REM "%A%" is like "ParentProcessId=2008"
echo %A:~16%Wednesday, July 24, 2013 10:24 PM -
My experience is that this will not work well in a highly concurrent environment as Tm.Kz described. It would seem that wmic falls all over itself writing TempWmicBatchFile.bat in the current directory. Or, it may be something else.
Friday, December 20, 2013 3:16 PM -
How will the %T% temp filename become unique?
Really interesting output. It appears to provide both UNIX and DOS-style line endings.
M:> wmic process where (Name="WMIC.exe" AND CommandLine LIKE "%%%TIME%%%") get ParentProcessId /value | odd
000000 0d 0d 0a 0d 0d 0a 50 61 72 65 6e 74 50 72 6f 63
\r \r \n \r \r \n P a r e n t P r o c
000010 65 73 73 49 64 3d 32 39 37 36 0d 0d 0a 0d 0d 0a
e s s I d = 2 9 7 6 \r \r \n \r \r \n
000020 0d 0d 0a 0d 0a
\r \r \n \r \n
000025Friday, December 20, 2013 3:22 PM