(yes, It''s horrible) IO.popen in terms of the non-blocking write
functions implemented in ruby 1.9 or 1.8.5. However, that''s not
necessarily such a great idea.
Alternatively, you could use a signal-handler for SIGCHLD and still use
IO.popen, but it seems difficult to ensure that you capture all of the
output from the command up until the point that the process exits.
You might use something like the (utterly horrible) code (written during
my lunch hour) I''ve attached below as a basis. If I have time,
I''ll have
a go at cleaning it up, and integrating it into puppet this weekend.
--------------000307080500050302090906
Content-Type: text/plain;
name="test.rb"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="test.rb"
class ChildExit < Exception
def initialize(command, status)
@status = status
@command = command
end
attr_reader :command, :status
end
def from_child(command)
output = ""
IO.popen(command) do |x|
begin
puts :BEGIN
old_sighandler = trap(:SIGCHLD) { raise ChildExit.new(command, $?) }
loop do
output << x.read(1)
puts [:readloop, output].inspect
end
rescue ChildExit => e
puts :RESCUE
trap(:SIGCHLD, old_sighandler)
puts [:select, IO.select([x], [], [], 0)].inspect
while IO.select([x], [], [], 0)
puts [:select, IO.select([x], [], [], 0)].inspect
chunk = x.read(1)
output << chunk if chunk
puts [:readfinal, chunk, output].inspect
break if not chunk
end
return output, e.status
finally
puts :FINALLY
trap(:SIGCHLD, old_sighandler)
end
end
end
puts from_child(''echo foo;echo -n foo; (sleep 10 ;echo bar)
&'').inspect
*****************************************************************************
This email and any attachments are strictly confidential and intended for the
addressee(s) only. If this email has been sent to you in error, please let us
know by forwarding it to us at support at scansafe.com.
Neither ScanSafe nor its directors, officers or employees accepts any liability
for the accuracy or completeness of this email. Unless expressly stated to the
contrary, no contracts may be concluded on behalf of ScanSafe by means of e-mail
communication.
*****************************************************************************
--------------000307080500050302090906--