From: Logan Capaldo Date: 2005-05-12T08:11:02+09:00 Subject: Re: Infinite loop program w/ IO.popen On 5/11/05, Venkat Venkataraju wrote: > Hi All > > I'm trying to write a ruby program that will constantly read a open > pipe, line by line and then log it onto another file. but when i try to > run this script with an & at the end, the program stops as soon as it > starts. > > here is the listing of the code. if the script is run normaly without > any & in the end, it works. > > #! /usr/bin/env ruby > # loop.rb: infinite loop program > > ostream = File.new("junk.log", "w") > pipe = IO.popen("top"); > > if $0 == __FILE__ > while true > ostream.puts(pipe.readline) > ostream.flush > sleep(1) > end > end > > I know that the reason for teh termination of the code has to do > somethign with the pipe. is this a normal behavior? > > i would like to execute this code without depending on the console. is > it possible to do so? or if not, is there any other solution? > > Thanks > Venkat > > #! /usr/bin/env ruby # loop.rb: infinite loop program Process.fork do ostream = File.new("junk.log", "w") pipe = IO.popen("top"); if $0 == __FILE__ while true ostream.puts(pipe.readline) ostream.flush sleep(1) end end end Does this version behave better? It should exit immediately but have a forked process doing its thing in the background with no need for an at the shell.