Infinite loop program w/ IO.popen

V

Venkat Venkataraju

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
 
L

Logan Capaldo

Hi All
=20
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.
=20
here is the listing of the code. if the script is run normaly without
any & in the end, it works.
=20
#! /usr/bin/env ruby
# loop.rb: infinite loop program
=20
ostream =3D File.new("junk.log", "w")
pipe =3D IO.popen("top");
=20
if $0 =3D=3D __FILE__
while true
ostream.puts(pipe.readline)
ostream.flush
sleep(1)
end
end
=20
I know that the reason for teh termination of the code has to do
somethign with the pipe. is this a normal behavior?
=20
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?
=20
Thanks
Venkat
=20
=20

#! /usr/bin/env ruby
# loop.rb: infinite loop program
Process.fork do
ostream =3D File.new("junk.log", "w")
pipe =3D IO.popen("top");

if $0 =3D=3D __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.
 
V

Venkat Venkataraju

Logan said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top