[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

at_exit handlers and Process.kill

Daniel Berger

4/15/2008 12:58:00 PM

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

21 Answers

Tomek Paczkowski

4/15/2008 1:22:00 PM

0

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?

I had to use all of them:

Signal.trap("KILL") { }
Signal.trap("EXIT") { }
Signal.trap("TERM") { }
Signal.trap("ABRT") { }

Use Signal.list to get a list of available signals to trap on you machine.

--
Oinopion
http:...

Daniel Berger

4/15/2008 1:36:00 PM

0



On Apr 15, 7:25 am, Tomek Paczkowski <oinop...@gmail.com> wrote:
> 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?
>
> I had to use all of them:
>
> Signal.trap("KILL") { }
> Signal.trap("EXIT") { }
> Signal.trap("TERM") { }
> Signal.trap("ABRT") { }
>
> Use Signal.list to get a list of available signals to trap on you machine.

Ok, what about the at_exit handler?

Thanks,

Dan

ts

4/15/2008 1:38:00 PM

0

Daniel Berger wrote:
> # process_test.rb
> File.open("pid.txt", "w"){ |fh| fh.print Process.pid }
> p Process.pid
>
> sleep 1 while true

you really think that the following line will be executed ?

>
> 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)

This is SIGTRAP


Guy Decoux

Daniel Berger

4/15/2008 4:29:00 PM

0

ts wrote:
> Daniel Berger wrote:
>> # process_test.rb
>> File.open("pid.txt", "w"){ |fh| fh.print Process.pid }
>> p Process.pid
>>
>> sleep 1 while true
>
> you really think that the following line will be executed ?

Why not? The docs say at_exit, "Converts block to a Proc object (and
therefore binds it at the point of call), and registers
it for execution when the program exits."

Well, the program exited, didn't it?

If this just can't work then I think there needs to be a way to handle
graceful cleanup without having to trap all the possible signals that
could kill the program.

Regards,

Dan

ts

4/15/2008 4:38:00 PM

0

Daniel Berger wrote:
>>> sleep 1 while true
>> you really think that the following line will be executed ?
>
> Why not?

Try this


ruby -e 'sleep 1 while true; p :ok'


and wait that it give :ok ...


Guy Decoux

Daniel Berger

4/15/2008 4:59:00 PM

0

ts wrote:
> Daniel Berger wrote:
>>>> sleep 1 while true
>>> you really think that the following line will be executed ?
>> Why not?
>
> Try this
>
>
> ruby -e 'sleep 1 while true; p :ok'
>
>
> and wait that it give :ok ...

Yes, Guy, I understand that. You aren't listening.

How do I create an all purpose cleanup handler within a Ruby program
that will fire off no matter how the script is terminated?

Thanks,

Dan


Ken Bloom

4/15/2008 5:08:00 PM

0

On Tue, 15 Apr 2008 08:35:35 -0500, Daniel Berger wrote:

> On Apr 15, 7:25 am, Tomek Paczkowski <oinop...@gmail.com> wrote:
>> 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?
>>
>> I had to use all of them:
>>
>> Signal.trap("KILL") { }
>> Signal.trap("EXIT") { }
>> Signal.trap("TERM") { }
>> Signal.trap("ABRT") { }
>>
>> Use Signal.list to get a list of available signals to trap on you
>> machine.
>
> Ok, what about the at_exit handler?

It will work if you put things in the right order. (See my other answer.)

--Ken

--
Ken (Chanoch) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu...

Ken Bloom

4/15/2008 5:08:00 PM

0

On Tue, 15 Apr 2008 11:28:33 -0500, Daniel Berger wrote:

> ts wrote:
>> Daniel Berger wrote:
>>> # process_test.rb
>>> File.open("pid.txt", "w"){ |fh| fh.print Process.pid } p Process.pid
>>>
>>> sleep 1 while true
>>
>> you really think that the following line will be executed ?
>
> Why not? The docs say at_exit, "Converts block to a Proc object (and
> therefore binds it at the point of call), and registers it for execution
> when the program exits."

Ruby does *everything* in the order it encounters it in the file.
at_exit installs an exit handler, (a proc to be run later) but it's a
method that has to be executed. If you put "sleep 1 while true" before
the at_exit call, the at_exit call will never be executed, so the exit
handler will never be installed. Adjust your program to call at_exit (or
whatever other handlers you want) before running anything else.

# process_test.rb
at_exit {
puts "AT_EXIT"
}

END{
puts "END"
}

trap("KILL"){
puts "KILLED"
}


File.open("pid.txt", "w"){ |fh| fh.print Process.pid }
p Process.pid

sleep 1 while true

--Ken

--
Ken (Chanoch) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu...

Tim Pease

4/15/2008 5:09:00 PM

0


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

ts

4/15/2008 5:09:00 PM

0

Daniel Berger wrote:
> How do I create an all purpose cleanup handler within a Ruby program
> that will fire off no matter how the script is terminated?

at_exit and trap the signal that you want but *please* do this
*before* 'sleep 1 while true'

moulon% ruby -e 'at_exit { puts :exit}; trap("TRAP") { puts :SIGTRAP;
exit }; sleep 1 while true'
SIGTRAP
exit
moulon%


Guy Decoux