From: "ara.t.howard" Date: 2008-06-03T01:46:45+09:00 Subject: Re: Traping signals on child processes On Jun 2, 2008, at 10:17 AM, Mário Lopes wrote: > > Tried that with the following example just for testing.. > > fork { exec "echo 'start'; sleep 20; echo 'end'" } > > And if I issue a SIGINT the warning gets printed but the 'end' never > gets printed meaning that the process ended abruptly. That's what I'm > trying to avoid :-/ > > Mário you have to avoid races, programs which install their own handlers (like ruby), and confusing the issue by shelling out three child processes in instead of one: cfp:~ > cat a.rb ready = :ready pipe = IO.pipe trap('INT'){ STDERR.puts "#{ Process.pid } killed" } pid = fork{ pipe.first.close pipe = pipe.last pipe.puts ready sleep } trap 'INT', 'DEFAULT' pipe.last.close pipe = pipe.first ready = pipe.gets STDERR.puts "parent: #{ Process.pid }" STDERR.puts "child: #{ pid }" Process.kill 'INT', pid Process.waitpid pid, Process::WUNTRACED cfp:~ > ruby a.rb parent: 25227 child: 25228 25228 killed basically ruby is, by default, going to relay the signal to the child, you need to catch it and do something sensible with it. a @ http://codeforpeople.com/ -- we can deny everything, except that we have the possibility of being better. simply reflect on that. h.h. the 14th dalai lama