Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re: Killing a process on Windows (Win32::Process question)

by tokpela (Chaplain)
on May 14, 2009 at 09:10 UTC ( #764035=note: print w/replies, xml ) Need Help??

in reply to Killing a process on Windows (Win32::Process question)

I think you might want to look at Win32::Job

This module will allow you to create process which can contain subprocesses. The module contains the ability to timeout after a period of time and then allows you to kill the process (and subprocesses).

From the documentation:

Windows 2000 introduced the concept of a "job": a collection of processes which can be controlled as a single unit. For example, you can reliably kill a process and all of its children by launching the process in a job, then telling Windows to kill all processes in the job. Win32::Job makes this feature available to Perl.

For example, imagine you want to allow 2 minutes for a process to complete. If you have control over the child process, you can probably just run it in the background, then poll every second to see if it has finished.

That's fine as long as the child process doesn't spawn any child processes. What if it does? If you wrote the child process yourself and made an effort to clean up your child processes before terminating, you don't have to worry. If not, you will leave hanging processes (called "zombie" processes in Unix).

With Win32::Job, just create a new Job, then use the job to spawn the child process. All its children will also be created in the new Job. When you time out, just call the job's kill() method and the entire process tree will be terminated.

  • Comment on Re: Killing a process on Windows (Win32::Process question)

Replies are listed 'Best First'.
Re^2: Killing a process on Windows (Win32::Process question)
by rovf (Priest) on May 14, 2009 at 09:43 UTC

    The problem is that the creation of subprocesses happens outside my Perl application. I basically get as input a string, which contains some Windows command (typically the name of a Batch file). I then need to create a job (I create a console for this), and this is the job which I want to kill when it runs for too long. The problem is that I have no idea what the batch file will be doing. It can call some EXE file which in turn creates tons of subprocesses. If I understand you right, Win32::Job won't help me in this case.

    Ronald Fischer <>
      If I understand you right, Win32::Job won't help me in this case.

      Actually, it probably will. According to the docs:

      By default, processes created using CreateProcess by a process associated with a job are associated with the job;

      Which mean if you create a job (a Win32::Job), and then associate the top level process you start with that job, then any processes it spawns will also be associated with that job. You should then be able to kill them all by killing the job.

      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

        Thanks a lot!!! I will try this alternative!

        Ronald Fischer <>

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://764035]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (12)
As of 2017-09-26 09:21 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (293 votes). Check out past polls.