Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

How do I kill a process tree in Windows

by DrWhy (Chaplain)
on Aug 18, 2005 at 15:57 UTC ( #484836=perlquestion: print w/ replies, xml ) Need Help??
DrWhy has asked for the wisdom of the Perl Monks concerning the following question:

Brothers and Sisters,

Being new to programming Perl under Windows I am constantly running into things that don't work the same as they do under Unix (e.g. fork which I've pretty much finally got my head around).

Here's another one that I'm not so sure of. I've super-searched PM and Googled a little, but can't seem to find what I need to know. I need to make sure that when some kills my Perl script that it kills all of it's project. I.e., I want to turn a ^c from a user into the equivalent of Proc::Killfam::killfam 'TERM', $$. I have used the following code to try to accomplish this with mixed success:

use Win32::Process::Info; my $pi = new Win32::Process::Info(); $SIG{INT} = sub { local $SIG{INT} = 'IGNORE'; @children = keys %{$pi->Subprocesses($$)}; @children = grep $$ != $_, @children; kill 1, @children; exit; };
This seems to work sometimes, but not others. In particular this hangs when one of the children is 'stuck' some way, e.g. in an infinite loop.

Does anyone here have other, better ideas about how to assure that the entire process tree is killed when you ^c the Perl script that started it all?

FWIW, I'm working mostly on Windows XP and 2003 Server, but I can't guarantee that these are the only Windows variants these scripts will need to work on.

--DrWhy

"If God had meant for us to think for ourselves he would have given us brains. Oh, wait..."

Comment on How do I kill a process tree in Windows
Download Code
Re: How do I kill a process tree in Windows
by JSchmitz (Canon) on Aug 18, 2005 at 18:59 UTC
    The below example is from a Win 2000 server. You should be able to use kill "process name" and it should smoke everything in the tree. Similar to "pkill" in Solaris. I am not a Win admin so I am not sure if they don't have some extra command line kit like ptools installed here or what but you may want to check into it.....(updated) In windows 2003 you have to use: tskill "process ID"
    c:\kill bpsched process bpsched.exe (860) - '' killed process bpsched.exe (852) - '' killed process bpsched.exe (4168) - '' killed
    Cheers -
    Jeffery
      So it sounds like you are saying there is no way to do it inside perl; I'll have to go to an external program. I didn't find anything named kill(\.exe)? on my XP box. A little research indicates that this was a tool that came with WIn2k, but not more recent OSs. I did find someone who had taken the code for that kill and updated to work with XP and to handle process trees. That's here.

      I'm still kind of hoping there's a good Perl solution out there so I don't have to be starting up external processes in a SIGINT handler. I took a second look at Proc::Killfam just in case it might work under Windows even if it isn't available via ppm. There seems to be some Cygwin compatibility in Proc::ProcessTable (which Proc::Killfam uses to figure out what processes need to be killed), but I don't know if that translates into Windows compatibility for P::K. I'm thinking not.

      --DrWhy

      "If God had meant for us to think for ourselves he would have given us brains. Oh, wait..."

        Unfortunately I don't know the answer to the Perl topic, however the builtin utility you want on XP is called "taskkill". Passing it the /T parameter is a treekill.
        Hi,

        If you spawn the processes yourself from within a perl script you should use Win32::Job . This will give you job control and thus the ability to kill a group of process.

        As i understand it, this is the way to control processes in Windows, that is killing a processes and all itīs sons.

        I find dealing with PID and PPIDS is kind of not adapted since PIDs numbers are recycled fastly and you could kill something else !

        In "real" windows programing there may be something like a "process handle" that you get when you create a process and that allow fine control, but within Perl Win32::Job seems the way to go ...

        ZlR.

        It's called a RESOURCE KIT, and you get it on CD with servers(usually), and can download it from microsoft Resource Kit

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://484836]
Approved by Nevtlathiel
Front-paged by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2014-08-21 03:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (127 votes), past polls