Skip to content

Instantly share code, notes, and snippets.

@synsa
Forked from sauloperez/signal_catching.rb
Created March 14, 2019 22:53
Show Gist options
  • Save synsa/5ee4ea0c049aea3f64afcac9baee4bf4 to your computer and use it in GitHub Desktop.
Save synsa/5ee4ea0c049aea3f64afcac9baee4bf4 to your computer and use it in GitHub Desktop.
How to catch SIGINT and SIGTERM signals in Ruby
# 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