Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: How to kill a windows process by command name?

by johnnywang (Priest)
on Nov 18, 2005 at 20:10 UTC ( [id://509933]=note: print w/replies, xml ) Need Help??


in reply to How to kill a windows process by command name?

Thanks to everyone, I figured this out, here's the code:
use strict; use Win32::Process::Info; use Win32::Process; Win32::Process::Info->Set(variant=>'WMI'); my $pi = Win32::Process::Info->new(); for($pi->ListPids){ my ($info) = $pi->GetProcInfo($_); if($info->{CommandLine} eq 'perl foo.pl a'){ my $obj; my $pid = $info->{ProcessId}; Win32::Process::Open($obj,$pid,1); $obj->Kill(0); print "Killed $pid\n\n"; } }

Replies are listed 'Best First'.
Re^2: How to kill a windows process by command name?
by BrowserUk (Patriarch) on Nov 18, 2005 at 20:54 UTC

    You could replace the use of Win32::Process with a call to perl's built-in kill using a signal value of 2 (or 21 or -9 work, as do many others).

    use strict; use Win32::Process::Info; Win32::Process::Info->Set(variant=>'WMI'); my $pi = Win32::Process::Info->new(); for($pi->ListPids){ my ($info) = $pi->GetProcInfo($_); if($info->{CommandLine} eq 'perl foo.pl a'){ my $obj; my $pid = $info->{ProcessId}; kill 2, $pid; print "Killed $pid\n\n"; } }

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2024-03-19 11:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found