What is the PID of the current cmd.exe

提議的解答 What is the PID of the current cmd.exe

  • Wednesday, March 14, 2007 9:30 PM
     
     

    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!

     

All Replies

  • Thursday, March 15, 2007 9:05 PM
     
     
    Hi, how do you launch the batch files? using the Exec task? and you need to track the PID of that batch process?
  • Thursday, December 06, 2007 5:53 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

     

  • Tuesday, August 25, 2009 8:06 PM
     
     Proposed Answer
    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, July 10, 2012 7:07 PM
     
     Proposed Answer Has Code

    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 %%b

    Output

    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
    
    

    • Edited by Tm.Kz Tuesday, July 10, 2012 7:07 PM
    • Edited by Tm.Kz Tuesday, July 10, 2012 7:09 PM
    • Edited by Tm.Kz Tuesday, July 10, 2012 7:14 PM
    • Edited by Tm.Kz Tuesday, July 10, 2012 7:15 PM
    • Edited by Tm.Kz Tuesday, July 10, 2012 7:16 PM
    • Proposed As Answer by Tm.Kz Tuesday, July 10, 2012 7:16 PM
    • Edited by Tm.Kz Tuesday, July 10, 2012 7:16 PM
    •  
  • Thursday, August 23, 2012 6:27 AM
     
     

    Nice. Thanks for the info