Skip to content

Instantly share code, notes, and snippets.

@AlanQuatermain
Created November 11, 2011 16:48
Show Gist options
  • Save AlanQuatermain/1358494 to your computer and use it in GitHub Desktop.
Save AlanQuatermain/1358494 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
require 'rubygems'
require 'open4'
# catch SIGCHLD (child process terminated) signals and handle them
trap("CHLD") do
begin
Process.wait(-1, Process::WNOHANG)
rescue Object
# catch exceptions, but don't do anything with them
end
end
input_data = File.read("test_file.csv")
# I want to pope the input data first through sort, then through my custom binary
# sort using regular popen
Open4.popen4("sort") do |pid, i, o, e|
i << input_data # does this complete, or does sort begin to block?
# now spawn the next item in the chain -- timeouts are optional
Open4.spawn "/path/to/binary", :stdin => o, :stdout => STDOUT, :stderr => STDERR, :stdin_timeout => 30.0
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment