Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
P is for Practical
 
PerlMonks  

Re: Kill a process in perl windows and proceed only if it killed the process

by GrandFather (Cardinal)
on Jul 19, 2012 at 22:11 UTC ( #982722=note: print w/ replies, xml ) Need Help??


in reply to Kill a process in perl windows and proceed only if it killed the process

Often it's not enough to kill a process, you need to kill a process tree. For example modern compilers often spawn additional processes to manage various parts of the compile and link processing. Another part of the problem can be that some processes may take a long time to close down or even may be prevented from closing due to things like dialogues that need to be closed. The following sub may help with the process tree part of the problem, but doesn't address the UI problem at all:

sub KillProcTree { my ($pid) = @_; if ($^O ne 'MSWin32') { return "Aborting" if !$pid || !kill 'HUP', $pid; return "Aborted"; } require 'Win32/Process/Info.pm'; require 'Win32/Process.pm'; Win32::Process::Info->import(); my $pi = Win32::Process::Info->new('', 'WMI'); my $iters; while (my %info = $pi->Subprocesses($pid)) { if (++$iters > 40) { Win32::Process::KillProcess($pid, -1); return 'Aborting'; } my @leaves = grep {!@{$info{$_}}} keys %info; last if !@leaves; print "Killing: @leaves\n"; Win32::Process::KillProcess($_, -1) for @leaves; sleep 2; # Allow a little time for processes to die } return 'Aborted'; }
True laziness is hard work


Comment on Re: Kill a process in perl windows and proceed only if it killed the process
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (13)
As of 2014-04-17 10:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (444 votes), past polls