Skip to content

Instantly share code, notes, and snippets.

@bradland
Last active August 29, 2015 14:08
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 bradland/08a0ffa3d92674556a13 to your computer and use it in GitHub Desktop.
Save bradland/08a0ffa3d92674556a13 to your computer and use it in GitHub Desktop.
Trap vs rescue for handling ctrl-C during script execution.
# Using a block allows you to limit the scope of the Interrupt handling code,
# as well as providing the opportunity to provide a meaningful exit code. For
# more information on exit codes, see: http://tldp.org/LDP/abs/html/exitcodes.html
begin
sleep 10
rescue Interrupt
$stderr.puts "Stopped sleeping..."
exit 130
end
# Calling Signal.trap will trap the specified signal now and forever! This
# means that if the code is required or loaded elsewhere, the signal will
# still be trapped and the message printed.
Signal.trap('INT') { $stderr.puts "Stopped sleeping..." }
sleep 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment