Infinite loop program w/ IO.popen

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.

···

On 5/11/05, Venkat Venkataraju <outofjungle@gmail.com> 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

Logan Capaldo wrote:

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.

It works!!! Thanks for the info.

I did find that all the programs that use stdin needs to be connected to
a tty. my main goal was to execute ssh command thru pipe, but i chose
"top" as an example as it had similar behavior.

so for ssh, all i have to pass -f or -n flag and it routes the stdin to
/dev/null and that fixed my issue.

Thanks
Venkat