# Looking for a method to kill all children for my Timeout::Error contingency

**URL:** <https://www.ruby-forum.com/t/looking-for-a-method-to-kill-all-children-for-my-timeout-error-contingency/163784>\
**Category:** Ruby\
**Created:** [March 24, 2009, 9:00pm UTC](https://www.ruby-forum.com/t/looking-for-a-method-to-kill-all-children-for-my-timeout-error-contingency/163784 "2009-03-24T21:00:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xeno\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/958977/32.png) [@Xeno\_C](https://www.ruby-forum.com/u/Xeno_C)\
**Post date:** [March 24, 2009, 9:00pm UTC](https://www.ruby-forum.com/t/looking-for-a-method-to-kill-all-children-for-my-timeout-error-contingency/163784/1 "2009-03-24T21:00:58Z")

</div>

Do I really need to collect all the pids from fork to kill them in that  
case, or  
is there a method I’m not finding in pickaxe that just does it???

xc

---

<div class="post-metadata">

**Author:** ![Xeno\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/958977/32.png) [@Xeno\_C](https://www.ruby-forum.com/u/Xeno_C)\
**Post date:** [March 24, 2009, 9:15pm UTC](https://www.ruby-forum.com/t/looking-for-a-method-to-kill-all-children-for-my-timeout-error-contingency/163784/2 "2009-03-24T21:15:03Z")

</div>

On Wed, Mar 25, 2009 at 04:57:03AM +0900, Xeno C. wrote:

> Do I really need to collect all the pids from fork to kill them in that case, or  
> is there a method I’m not finding in pickaxe that just does it???

Here’s a trick I use, maybe it is useful. Note that even if  
push-to-slaves  
creates children, those too will be killed because they reside in the  
same  
process group.

require ‘timeout’  
signal = ‘-TERM’  
timeout = 30

pid = Process.fork do  
Process.setsid  
exec %{push-to-slaves -v}  
end  
begin  
Timeout::timeout(timeout) { Process.waitpid(pid) }  
rescue Timeout::Error =\> exc  
puts “#{exc.message}, killing pid #{pid}”  
Process.kill(signal, pid)  
res = Process.waitpid(pid)  
puts “pid #{pid.inspect} != res #{res.inspect}” if res != pid  
end

---

<div class="post-metadata">

**Author:** ![Xeno\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/958977/32.png) [@Xeno\_C](https://www.ruby-forum.com/u/Xeno_C)\
**Post date:** [March 24, 2009, 9:27pm UTC](https://www.ruby-forum.com/t/looking-for-a-method-to-kill-all-children-for-my-timeout-error-contingency/163784/3 "2009-03-24T21:27:58Z")

</div>

Jos B. wrote:

> On Wed, Mar 25, 2009 at 04:57:03AM +0900, Xeno C. wrote:
> 
> > Do I really need to collect all the pids from fork to kill them in that case, or  
> > is there a method I’m not finding in pickaxe that just does it???
> 
> Here’s a trick I use, maybe it is useful. Note that even if push-to-slaves  
> creates children, those too will be killed because they reside in the same  
> process group.

The problem with that is I have many processes, so I would be doing this  
with  
arrays and loops, which suddenly becomes problematic. If I could just  
call  
something that automatically kills the entire tree of children on down  
that  
would be much nicer…??

---

<div class="post-metadata">

**Author:** ![Xeno\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/958977/32.png) [@Xeno\_C](https://www.ruby-forum.com/u/Xeno_C)\
**Post date:** [March 25, 2009, 6:20am UTC](https://www.ruby-forum.com/t/looking-for-a-method-to-kill-all-children-for-my-timeout-error-contingency/163784/4 "2009-03-25T06:20:42Z")

</div>

On Wed, Mar 25, 2009 at 09:56:53AM +0900, Mike B wrote:

> > The problem with that is I have many processes, so I would be doing this with  
> > arrays and loops, which suddenly becomes problematic. If I could just call  
> > something that automatically kills the entire tree of children on down that  
> > would be much nicer…??

If those many processes were being started by the push-to-slaves script  
as in  
the example I posted, unless that script did something to create new  
process  
groups for its children, the code I posted would terminate all those  
processes  
because they all belong to the same process group which is targeted by  
the  
Process.kill(-TERM, pid) invocation. Iow, if you want to kill all the  
children  
at once the easiest way is to make sure they all belong to the same  
pgrp.

---

<div class="post-metadata">

**Author:** ![Xeno\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/958977/32.png) [@Xeno\_C](https://www.ruby-forum.com/u/Xeno_C)\
**Post date:** [March 25, 2009, 2:00am UTC](https://www.ruby-forum.com/t/looking-for-a-method-to-kill-all-children-for-my-timeout-error-contingency/163784/5 "2009-03-25T02:00:53Z")

</div>

On Mar 24, 12:23 pm, Xeno C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> arrays and loops, which suddenly becomes problematic. If I could just call
> 
> > ```
> > puts "pid #{pid.inspect} != res #{res.inspect}" if res != pid
> > 
> > ```
> > 
> > end
> 
> –  
> “Facts do not cease to exist because they are ignored” - Aldous Huxley  
> “…Or I might add, because they are denied” - Dr. Albert A. Bartletthttp://www.eskimo.com/~xeno

Maybe this could help?

[http://markmail.org/message/3nl2idghbekgbahb](http://markmail.org/message/3nl2idghbekgbahb)

It’s a link to Ara Howards announcement for terminator.

Mike B.

---

<div class="post-metadata">

**Author:** ![Xeno\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/958977/32.png) [@Xeno\_C](https://www.ruby-forum.com/u/Xeno_C)\
**Post date:** [March 25, 2009, 6:50pm UTC](https://www.ruby-forum.com/t/looking-for-a-method-to-kill-all-children-for-my-timeout-error-contingency/163784/6 "2009-03-25T18:50:40Z")

</div>

Jos B. wrote:

> On Wed, Mar 25, 2009 at 09:56:53AM +0900, Mike B wrote:
> 
> > On Mar 24, 12:23 pm, Xeno C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> > 
> > > Jos B. wrote:

Yes thank you.

Part of my problem is I wasn’t feeling sure about how I would use  
processes or  
threads, and why. With the advent of the new Chrome Philosophy, I was  
thinking  
perhaps processes might be the way to go, as I was doing curl  
executions, but it  
turns out I need to do a surrounding sequence of things in parallel for  
initializations and closures, so I am leaning back to threads now, which  
I think  
solves the problems. I would sure like to find a book just on use of  
threads  
and processes in Ruby. I would think a knowledgeable person could  
probably write  
at least 12 chapters on it for the layman alone, and not having done  
some of  
this for over ten years, that’s where I’m feeling I’m at.

Thank you again for the feedback.

Sincerely, Xeno

xc
