Class: Utils::Subprocess
- Inherits:
-
Object
- Object
- Utils::Subprocess
- Defined in:
- lib/meshx-plugin-sdk.rb
Instance Method Summary collapse
-
#initialize(cmd, &block) ⇒ Subprocess
constructor
A new instance of Subprocess.
Constructor Details
#initialize(cmd, &block) ⇒ Subprocess
Returns a new instance of Subprocess.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/meshx-plugin-sdk.rb', line 42 def initialize(cmd, &block) Open3.popen3(cmd) do |stdin, stdout, stderr, thread| { :out => stdout, :err => stderr }.each do |key, stream| Thread.new do until (line = stream.gets).nil? 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 exit_code = thread.value if (exit_code != 0) puts("Failed to execute_cmd #{cmd} exit code: #{exit_code}") Kernel.exit(false) end end end |