Created
September 2, 2014 22:41
-
-
Save zpatten/bc896581140b17eee4af to your computer and use it in GitHub Desktop.
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", "WINCH"] | |
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) | |
begin | |
trap signal do | |
begin | |
kill signal, $child_pid if $child_pid | |
rescue Errno::ESRCH | |
end | |
end | |
rescue Exception => e | |
$stderr.puts "Caught exception #{e}" | |
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