From: Nate Murray Date: 2006-11-21T05:35:07+09:00 Subject: Re: fork, trap and exec. Pickaxe typo? Yeah, but how do I wait for an unknown amount of time? It would seem that this would do it: trap("CLD") { pid = Process.wait puts "Child pid #{pid}: terminated" } 1.upto(4) do fork do exec("sleep 3 && echo \"hello $HOSTNAME #{Process.pid}\n\"") end end Process.waitall But trap isnt always called on all the children. Example output: hello nate.cpo 4136 Child pid 4136: terminated hello nate.cpo 4139 Child pid 4139: terminated hello nate.cpo 4137 hello nate.cpo 4138 Child pid 4138: terminated Notice that 4137 never called the trap callback even though Process.waitall was called. Hugh Sasse wrote: > On Tue, 21 Nov 2006, Nate Murray wrote: > > > Hey All, I got this code straight from the PickAxe: > > > > But it doesnt word! > > The CLD trap'ped call never executes. > > > > > > Another question... If I perform the exec 4 times the trap call back > > should be called four times, right? > > > > So this code: > > > > trap("CLD") { > > pid = Process.wait > > puts "Child pid #{pid}: terminated" > # exit > > } > > > > 1.upto(4) do > > exec("echo \"hello $HOSTNAME #{Process.pid}\n\" ; sleep 5") if > > fork.nil? > > end > > sleep 25 > > > > Should call trap("CLD") four times, right? > > > > Thanks for your help in advance. > > that should do the job. You didn't wait around for things to happen. > Hugh > > > > > >