Skip to content

Instantly share code, notes, and snippets.

@bradland
Last active Aug 29, 2015
Embed
What would you like to do?
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