From: Robert Klemme Date: 2007-02-22T19:20:09+09:00 Subject: Re: ruby tail On 22.02.2007 10:59, Alex Young wrote: > Kevin Jackson wrote: >>> Actually, I am not sure how this would work. If I do this, I get no >>> output from the file: >>> >>> `tail -f sim.out` >>> >>> So there is something basic I am missing here. Any help? >> >> it shells out to the process and waits for it to return - with -f does >> tail ever return? if not you may not be able to use it - AFAIK ` and >> exec are the same - someone with better ruby knowledge may be able to >> offer a solution - perhaps using threads or something > Use IO.popen: > > IO.popen('tail -f sim.out'){|f| > while line = f.gets > p line > end > } You can do it in pure Ruby: 11:14:14 [Temp]: rm x; for ((i=0;i<30;++i)); do echo $i; sleep 0.2; done > x & ruby tail.rb removed `x' [1] 2252 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 [1]+ Done for ((i=0; i<30; ++i)) do echo $i; sleep 0.2; done > x tail.rb:12:in `sleep': Interrupt from tail.rb:12:in `tail' from tail.rb:5:in `loop' from tail.rb:5:in `tail' from tail.rb:16 11:14:31 [Temp]: cat tail.rb # we never return! def tail(file) io = File.open(file) loop do while ( line = io.gets ) puts line end # uncomment next to watch what is happening # puts "-" sleep 1 end end tail "x" 11:14:35 [Temp]: Kind regards robert