Created
June 28, 2013 01:21
-
-
Save eatnumber1/5881754 to your computer and use it in GitHub Desktop.
Ignore signals without modifying your program.
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
#!/usr/bin/env ruby | |
require 'getoptlong' | |
include Process | |
include Signal | |
$ignored_signals = ["CLD"] | |
def usage(strm) | |
strm.puts <<-EOF | |
Usage: #{$0} [-h] [-s sig] [--signal=sig] [--help] [--] command [arguments...] | |
EOF | |
end | |
opts = GetoptLong.new( | |
['--signal', '-s', GetoptLong::REQUIRED_ARGUMENT], | |
['--help', '-h', GetoptLong::NO_ARGUMENT] | |
) | |
opts.each do |opt, arg| | |
case opt | |
when '--signal' | |
$ignored_signals << arg | |
when '--help' | |
usage $stdout | |
exit true | |
end | |
end | |
if ARGV.length < 1 | |
usage $stderr | |
exit false | |
end | |
$ignored_signals.each do |signal| | |
unless Signal.list.include?(signal) | |
$stderr.puts "Signal \"#{signal}\" not found" | |
exit false | |
end | |
end | |
$child_pid = nil | |
Signal.list.each do |(signal, _)| | |
next if $ignored_signals.include?(signal) | |
trap signal do | |
begin | |
kill signal, $child_pid if $child_pid | |
rescue Errno::ESRCH | |
end | |
end | |
end | |
$child_pid = fork do | |
setpgrp | |
exec ARGV[0], *ARGV[1, ARGV.length - 1] | |
end | |
waitpid $child_pid until $? && $?.exitstatus | |
exit $?.exitstatus |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment