[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Infinite loop program w/ IO.popen

Venkat Venkataraju

5/11/2005 8:34:00 PM

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


2 Answers

Logan Capaldo

5/11/2005 11:11:00 PM

0

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
>
>

#! /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.


Venkat Venkataraju

5/12/2005 1:17:00 AM

0

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