Skip to content

Instantly share code, notes, and snippets.

Last active Aug 29, 2015
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:
sleep 10
rescue Interrupt
$stderr.puts "Stopped sleeping..."
exit 130
# 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