Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Monks I need your help!
I looking for a module/procedure/code in perl that allows me to find, on Win32, the specific pid for an executable that is currently running. I saw @info = $pi->GetProcInfo (); in Win32::Process::Info which supposedly gets all the pids and throws them into a hash. I do obviously know the name of the executable that I am searching the pid for, but I would have no idea as to how to pull the specific pid out of this hash alone. There has got to be something simpler or more direct. Again, all I am looking for is just the *one* pid associated with the single executable that I am interested in. Let's say the executable's name is tracker.exe, I need to find the pid for just tracker.exe
Your help is greatly appreciated! Thanks in advance!

Replies are listed 'Best First'.
Re: find PID for a process/executable running on Win32
by BrowserUk (Patriarch) on Aug 09, 2004 at 13:10 UTC

    How much easier do you want it :)

    #! perl -slw use strict; use Win32::Process::Info; print "My pid: $$"; my $pi = Win32::Process::Info->new; my @info = $pi->GetProcInfo(); my @procsOfInterest = grep{ $_->{Name} eq $ARGV[ 0 ] } @info; if( @procsOfInterest ) { print "A process with the name $ARGV[ 0 ] was found with pid:", $_->{ProcessId} for @procsOfInterest; } else { print "No process with the name $ARGV[ 0 ] was found"; } __END__ P:\test>381203 perl.exe My pid: 1184 A process with the name perl.exe was found with pid:1184 P:\test>start /b perl -le"sleep 60; print 'Bye'" P:\test>381203 perl.exe My pid: 340 A process with the name perl.exe was found with pid:1600 A process with the name perl.exe was found with pid:340

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
      Righteous! sometimes the most obvious & simple solutions stand before us jumping up and down saying:
      'see me how obvious am I!!'

      Thanks for sharing your wisdom!