Here is an example of waiting for some output from a child process:
def run_and_wait_for_this regexp_to_wait_for, *cmd rd, wr = IO.pipe pid = Process.spawn(*cmd, out: wr) pid_waiter = Thread.new { Process.wait(pid); wr.close } thr = Thread.new do buffer = '' until buffer =~ regexp_to_wait_for buffer << rd.readpartial(100) end end thr.join rescue EOFError ensure rd.close end run_and_wait_for_this( /waiting for connections on port xxxx/, 'mongo', '--port', port, '--dbpath', MONGODB_PATH, '--nojournal' )
It is blocked until the process displays the expected result in the channel.
source share