Skip to content

Instantly share code, notes, and snippets.

@schneems
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schneems/41a30bd72153c6782746 to your computer and use it in GitHub Desktop.
Save schneems/41a30bd72153c6782746 to your computer and use it in GitHub Desktop.
def prepend_handler(signal, &handler)
previous = Signal.trap(signal) do
previous = -> { raise SignalException, signal } unless previous.respond_to?(:call)
handler.call(previous)
end
end
prepend_handler("TERM") do |old|
do_something
old.call
end
@JoshCheek
Copy link

I've handled this by resetting the old handler and then resending the signal:

$ ruby -e '
   old = trap("INT") {
     puts "\e[35mWhy hello, thar!\e[39m"
     trap "INT", old
     Process.kill "INT", Process.pid
   }

   Process.kill "INT", Process.pid
'
Why hello, thar!
-e:8:in `kill': Interrupt
    from -e:8:in `<main>'
fish: Job 6, 'ruby -e '' terminated by signal SIGINT (Quit request from job control (^C))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment