Ross Bamford
1/26/2006 10:45:00 AM
On Thu, 2006-01-26 at 07:06 +0900, tsuraan wrote:
> My test code is:
>
> #!/usr/bin/env ruby
> begin
> sleep(1000)
> ensure
> p "hi there!"
> end
>
> And I run the script and send it all sort of signals (quit, int, term,
> hup, etc). Nothing makes the program print "hi there!" Can this be
> done?
>
Running your code and hitting Ctrl-C does give the expected behaviour,
as does sending an INT directly:
[rosco@jukebox ruby]$ ruby term1.rb &
[1] 28052
[rosco@jukebox ruby]$ kill -INT 28052
[rosco@jukebox ruby]$
"hi there!"
term1.rb:2:in `sleep': Interrupt
from term1.rb:2
[1]+ Exit 1 ruby term1.rb
(I made it p "\nhi there!" to make the example clearer). If you wanted
to broaden your horizons a bit you could try trapping some signals and
raising exceptions yourself:
trap('TERM') {
raise "Terminated!"
}
begin
sleep 1000
rescue Exception => ex
puts "\nGot #{ex}"
ensure
puts "hi there!"
end
Running that gives:
[rosco@jukebox ruby]$ ruby term2.rb &
[1] 28088
[rosco@jukebox ruby]$ kill -TERM 28088
[rosco@jukebox ruby]$
Got Terminated!
hi there!
[1]+ Done ruby term2.rb
Obviously it's pretty simplistic but maybe gives a starting point...
Hope that helps,
Ross
--
Ross Bamford - rosco@roscopeco.REMOVE.co.uk