Last active
August 29, 2015 14:08
-
-
Save bradland/08a0ffa3d92674556a13 to your computer and use it in GitHub Desktop.
Trap vs rescue for handling ctrl-C during script execution.
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
# 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 |
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
# 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