Skip to content

Instantly share code, notes, and snippets.

@zpatten
Created September 2, 2014 22:41
Show Gist options
  • Save zpatten/bc896581140b17eee4af to your computer and use it in GitHub Desktop.
Save zpatten/bc896581140b17eee4af to your computer and use it in GitHub Desktop.
#!/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