-
-
Save niquepa/5838af288cd05ffaf3701a35192c9fe8 to your computer and use it in GitHub Desktop.
How to catch SIGINT and SIGTERM signals in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Signal catching | |
def shut_down | |
puts "\nShutting down gracefully..." | |
sleep 1 | |
end | |
puts "I have PID #{Process.pid}" | |
# Trap ^C | |
Signal.trap("INT") { | |
shut_down | |
exit | |
} | |
# Trap `Kill ` | |
Signal.trap("TERM") { | |
shut_down | |
exit | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Really cool. You can utilize Ruby's catch-throw, if you want to trap the signal and just continue, as if you have rescued it.