Re: at_exit handlers and Process.kill



Ken Bloom wrote:
On Tue, 15 Apr 2008 12:29:01 -0500, Daniel Berger wrote:

Ken Bloom wrote:
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

Right, I should have clarified that, even with the right ordering,
at_exit and END blocks weren't called.

Perhaps it's the signal I used? I haven't fully experimented yet...

Regards,

Dan

See signal(7) for descriptions and numbers of the various
signals. Most
likely you want to use SIGINT (signal 1), and then at_exit will be

SIGINT is 2, 1 is SIGHUP

sufficient. Note that SIGKILL (signal 9) cannot be caught under any
circumstances (it's used to kill badly misbehaving
applications), so your
sigkill handler is useless.

I think it's unnecessary to install lots of signal handlers to do the
cleanup. Just learn what each signal does, and what to expect, and
at_exit should already be designed to do what you want.

--Ken


.



Relevant Pages

  • Re: at_exit handlers and Process.kill
    ... execution when the program exits." ... If you put "sleep 1 while true" before ... handler will never be installed. ... See signalfor descriptions and numbers of the various signals. ...
    (comp.lang.ruby)
  • Re: measureing host loads every 10 sec until an application is finished.
    ... > load average during the application execution to a file right after ... Install handler for SIGCHLD signal - in the handler you just set ... When sleep ends ... calculate your load average, ...
    (comp.unix.programmer)
  • Re: at_exit handlers and Process.kill
    ... therefore binds it at the point of call), and registers it for execution ... If you put "sleep 1 while true" before ... handler will never be installed. ... puts "AT_EXIT" ...
    (comp.lang.ruby)
  • Re: at_exit handlers and Process.kill
    ... therefore binds it at the point of call), and registers it for execution ... 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. ...
    (comp.lang.ruby)
  • Re: A question about signal function
    ... > System V and BSD took different approaches to making signals reliable. ... > install a reliable handler, so that a handler blocked the coresponding ... I just found an interesting result, `sleep' doesn't works well in signal ...
    (comp.unix.programmer)