SysAdmin's, How to Get Process ID on Windows?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • WiredGuy
    Pounding Googlebot
    • Aug 2002
    • 34517

    #1

    SysAdmin's, How to Get Process ID on Windows?

    I'm converting a linux shell script into a Windows batch file and I'm having a hard time figuring out how to return the process ID of a program I just launched. I run this program in an infinite loop in the batch script so I need to get the process ID right after I launch it to keep track of which pid is with which session (so dumping tasklist won't work).

    Unix Example:
    java -jar Keywords.jar &
    echo $!

    On Linux $! is the pid. This is being launched from a batch file so how can I capture the pid on a Windows environment right after I launch?
    WG
    Last edited by WiredGuy; 07-23-2009, 08:52 AM.
    I play with Google.
  • Ice
    Confirmed User
    • Nov 2002
    • 26053

    #2
    Can you hit me up on icq
    icq 1904905

    Comment

    • Machete_
      WINNING!
      • Oct 2002
      • 14579

      #3
      you can do it using http://www.autohotkey.com/

      run, %v_program% newpid

      And then pick it up using ahk_pid %newpid%

      Comment

      • pstation
        Confirmed User
        • Jul 2003
        • 1135

        #4
        I dont believe there's an easy way to do this, from a straight batch script at least.

        I suppose you could write a powershell script and use System.Diagnostics.Process to basically encapsulate the process and control it that way. then you could just call process.Start() and process.Close()
        Last edited by pstation; 07-23-2009, 09:11 AM.

        Comment

        • Machete_
          WINNING!
          • Oct 2002
          • 14579

          #5
          or if you are doing it on a server, you can use Powershell http://blogs.msdn.com/powershell/arc...owershell.aspx

          Comment

          • WiredGuy
            Pounding Googlebot
            • Aug 2002
            • 34517

            #6
            I was really hoping on finding a simple Windows command to echo the pid rather than rewriting this all in powershell.
            WG
            I play with Google.

            Comment

            • pstation
              Confirmed User
              • Jul 2003
              • 1135

              #7
              no need to rewrite it entirely.

              you can just run something like ...

              powershell "$p = [diagnostics.process]::start('calc.exe');echo $p.Id"

              straight from cmd.exe or your batch file and it'll start the process and spit out the pid

              Comment

              Working...