Tim Pease
4/15/2008 5:09:00 PM
On Apr 15, 2008, at 6:58 AM, Daniel Berger wrote:
> Hi all,
>
> Ruby 1.8.6 p114
> OS X 10.4
>
> None of these handlers get picked up when I kill the
> 'process_test.rb' program with an external program. Why not?
>
> # process_test.rb
> File.open("pid.txt", "w"){ |fh| fh.print Process.pid }
> p Process.pid
>
> sleep 1 while true
>
> at_exit {
> puts "AT_EXIT"
> }
>
> END{
> puts "END"
> }
>
> trap("KILL"){
> puts "KILLED"
> }
>
> # kill_test.rb
> pid = IO.read("pid.txt").to_i
> p pid
> Process.kill(5, pid)
>
> Dan
>
This works for me on OS 10.5.2 and ruby-1.8.6-p111
$ cat runner.rb
File.open('pid.txt', 'w') {|fd| fd.puts Process.pid}
puts Process.pid
at_exit {$stderr.puts "AT_EXIT"}
END {$stderr.puts "END"}
Signal.trap(5) {$stderr.puts "KILLED"}
loop {sleep 1}
$ cat killer.rb
pid = Integer(File.read('pid.txt'))
puts pid
Process.kill(5,pid)
And when I run the killer script the runner script dies and outputs
"KILLED" to stderr.
The only real difference was using the explicit signal number (5) and
putting the infinite sleep after all the exit handlers were
registered. The only handler that was run was the block handling the
trap. But the END block and the at_exit block were not run. I would
expect that since a sigkill is pretty severe.
Blessings,
TwP