http://www.perlmonks.org?node_id=982722


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