class Utils::Subprocess

Public Class Methods

new(cmd) { |line, nil, thread| ... } click to toggle source
# File lib/ssh_scan/subprocess.rb, line 5
def initialize(cmd, &block)
  # see: http://stackoverflow.com/a/1162850/83386
  Open3.popen3(cmd) do |stdin, stdout, stderr, thread|
    # read each stream from a new thread
    { :out => stdout, :err => stderr }.each do |key, stream|
      Thread.new do
        until (line = stream.gets).nil? do
          # yield the block depending on the stream
          if key == :out
            yield line, nil, thread if block_given?
          else
            yield nil, line, thread if block_given?
          end
        end
      end
    end

    thread.join # don't exit until the external process is done
  end
end