From: Gennady Bystritsky Date: 2008-08-29T07:16:53+09:00 Subject: Re: open3 and background processes > -----Original Message----- > From: Daniel Berger [mailto:djberg96@gmail.com] > Sent: Tuesday, August 26, 2008 1:09 PM > To: ruby-talk ML > Subject: Re: open3 and background processes > > > > On Aug 26, 1:37 pm, Gennady Bystritsky > wrote: > > IO.readlines will try to read out everything until EOF, hence it > "hangs" for long running background processes. Can you use IO.readline > instead and modify your spawned program to output a one line message to > indicate success? Then it will catch both errors and successful runs. > > Ah, ok. This seems to work fine: > > program = File.join(Dir.pwd, "miniserver.rb") > > cmd = "ruby #{program} &" > > Open3.popen3(cmd) do |stdin, stdout, stderr| > # Use readline, since readlines waits until EOF > error = stderr.readline The problem here is that if your miniserver.rb does not output anything to stderr in case of no error, stderr.readline will hang as well until it gets a newline. What I meant in my previous reply is that miniserver.rb must output something like "SUCCESS" to stderr so that you can test for this in popen3 block: error = stderr.readline unless error.chomp == "SUCCESS # process errors here ... end > > if error > error = [error] << stderr.readlines # Get the rest > end > > puts error if error > end > > puts "Done" > > Many thanks, > > Dan