Skip to content

Instantly share code, notes, and snippets.

@chorn
Last active August 29, 2015 14:23
Show Gist options
  • Save chorn/bf0c82058880f2fb1d0a to your computer and use it in GitHub Desktop.
Save chorn/bf0c82058880f2fb1d0a to your computer and use it in GitHub Desktop.
popen4 woo hoo
require 'open4'
module Derp
def self.system(*args)
raise "No arguments specified." unless args && args.class == Array && args.size > 0
# This style of calling system allows for redirection and pipes, where the normal array style does not.
command = (args.flatten.compact.map{|a| a.match(/ /) ? "\"#{a}\"" : a }).join(' ')
puts command if $DEBUG
pid, stdin, stdout, stderr = Open4::popen4 command
stdin.close
waitpid, status = Process::waitpid2 pid
unless status.success?
puts "The following system call failed with return code #{status.exitstatus}"
puts command
puts "pid : #{ pid }"
puts "stdout : #{ stdout.read.strip }"
puts "stderr : #{ stderr.read.strip }"
puts "waitpid : #{ waitpid }"
end
status
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment