Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: find out, if process with $PID exists (under Windows!)

by AcidHawk (Vicar)
on Apr 09, 2003 at 14:03 UTC ( [id://249217]=note: print w/replies, xml ) Need Help??


in reply to find out, if process with $PID exists (under Windows!)

Try something like

#!/usr/bin/perl use strict; use warnings; use Win32::OLE qw( in ); use Win32::OLE::Variant; use Win32::Process; my $CLASS = "winmgmts:{impersonationLevel=impersonate}\\\\$ENV{COMPUTE +RNAME}\\Root\\cimv2"; my $WMI = Win32::OLE->GetObject( $CLASS ) || die "Cannot Get list of P +IDS\n"; foreach my $Proc ( sort {lc $a->{Name} cmp lc $b->{Name}} in( $WMI->In +stancesOf( "Win32_Process" ) ) ) { if ($Proc->{Name} eq "Whatever you are looking for.exe") { print "$Proc->{ProcessID} is STILL RUNNING\n"; # We Dont Want to KILL the PID ---Win32::Process::KillProcess( +$Proc->{ProcessID}, 5); # Other Processing Here..;-) } }
If on the other hand you already know the PID you can simply run
use Win32::process; Win32::Process::KillProcess($PID,5);
Keep in mind though that $PID is a perlvar and if I ma correct returns the PID of perl.exe that is running your current script. Maybe you should change your var name..

    From the perlvar help - ($PID/$$) "The process number of the Perl running this script. You should consider this variable read-only, although it will be altered across fork() calls."

Code Ripped from a working script I have running... This peice has not been tested on its own but should work..;-)

Update: Thanks to BrowserUk for reminding me what the question really was..!

-----
Of all the things I've lost in my life, its my mind I miss the most.

Replies are listed 'Best First'.
Re: Re: find out, if process with $PID exists (under Windows!)
by BrowserUk (Patriarch) on Apr 09, 2003 at 14:36 UTC

    The OP didn't want to kill a specific process, only find out if it was still running.


    Examine what is said, not who speaks.
    1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
    2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
    3) Any sufficiently advanced technology is indistinguishable from magic.
    Arthur C. Clarke.

      OOPS... I did read that, but got carried away with ripping a peice of code that I had previously put together that I forgot what the question was... :-(

      My First piece of code still works just don't do the kill peice...;)

      -----
      Of all the things I've lost in my life, its my mind I miss the most.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://249217]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-04-24 08:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found