Skip to content

Instantly share code, notes, and snippets.

@tkareine
Created July 6, 2010 22:32
Show Gist options
  • Save tkareine/466019 to your computer and use it in GitHub Desktop.
Save tkareine/466019 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
def exit_upon(*signals)
process_going_down = false
signals.map { |signal| signal.to_s.upcase }.each do |signal|
Signal.trap(signal) do
exit! if process_going_down # terminate if still executing the given block
process_going_down = true
yield(signal) if block_given?
exit! 0
end
end
end
exit_upon(:int, :term) do |signal|
puts "Received #{signal}, starting to do a long cleanup work..."
sleep 10
puts "Done cleaning up, exiting..."
end
sleep 1000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment