# Re: ruby tail

**URL:** <https://www.ruby-forum.com/t/re-ruby-tail/86446>\
**Category:** Ruby\
**Created:** [February 22, 2007, 10:43am UTC](https://www.ruby-forum.com/t/re-ruby-tail/86446 "2007-02-22T10:43:03Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Phy\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a587f6/32.png) [@Phy\_P](https://www.ruby-forum.com/u/Phy_P)\
**Post date:** [February 22, 2007, 10:43am UTC](https://www.ruby-forum.com/t/re-ruby-tail/86446/1 "2007-02-22T10:43:03Z")

</div>

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?

TIA,  
Phy

----- Original Message ----  
From: Kevin J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: ruby-talk ML [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Thursday, February 22, 2007 1:33:19 AM  
Subject: Re: ruby tail

> Is there a gem or perhaps a method I am not aware of that does the similar function of tail? Or is there a way to use tail in ruby? I have a file that I want to process as new data is appended to the file. This is easy enough using tail and tail has the added advantage that it can follow a file if it is “rolled”.

Just call out to tail

def tail(f)  
`tail f`  
end

or similar - if tail does what you want, use it

Kev

---

<div class="post-metadata">

**Author:** ![Phy\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a587f6/32.png) [@Phy\_P](https://www.ruby-forum.com/u/Phy_P)\
**Post date:** [February 22, 2007, 10:50am UTC](https://www.ruby-forum.com/t/re-ruby-tail/86446/2 "2007-02-22T10:50:04Z")

</div>

> 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

Kev

---

<div class="post-metadata">

**Author:** ![Phy\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a587f6/32.png) [@Phy\_P](https://www.ruby-forum.com/u/Phy_P)\
**Post date:** [February 22, 2007, 11:00am UTC](https://www.ruby-forum.com/t/re-ruby-tail/86446/3 "2007-02-22T11:00:05Z")

</div>

Kevin J. wrote:

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

---

<div class="post-metadata">

**Author:** ![Phy\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a587f6/32.png) [@Phy\_P](https://www.ruby-forum.com/u/Phy_P)\
**Post date:** [February 22, 2007, 11:14am UTC](https://www.ruby-forum.com/t/re-ruby-tail/86446/4 "2007-02-22T11:14:07Z")

</div>

removed\_email\_address@domain.invalid wrote:

> > > offer a solution - perhaps using threads or something

I haven’t ever done that, but it looks like it from the docs…

---

<div class="post-metadata">

**Author:** ![Phy\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a587f6/32.png) [@Phy\_P](https://www.ruby-forum.com/u/Phy_P)\
**Post date:** [February 22, 2007, 11:06am UTC](https://www.ruby-forum.com/t/re-ruby-tail/86446/5 "2007-02-22T11:06:06Z")

</div>

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

Out of interest, the blocking call is “gets” here, right? Can you use  
Kernel.select on a bunch of those handles?

Cheers,  
Benj

---

<div class="post-metadata">

**Author:** ![Phy\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a587f6/32.png) [@Phy\_P](https://www.ruby-forum.com/u/Phy_P)\
**Post date:** [February 22, 2007, 11:22am UTC](https://www.ruby-forum.com/t/re-ruby-tail/86446/6 "2007-02-22T11:22:10Z")

</div>

On 22.02.2007 10:59, Alex Y. wrote:

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

---

<div class="post-metadata">

**Author:** ![Phy\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a587f6/32.png) [@Phy\_P](https://www.ruby-forum.com/u/Phy_P)\
**Post date:** [February 22, 2007, 2:15pm UTC](https://www.ruby-forum.com/t/re-ruby-tail/86446/7 "2007-02-22T14:15:30Z")

</div>

This version is safer and a tad more flexible, just in case there is an  
exception (e.g. because of thread interruption):

def tail(file, interval=1)  
raise “Illegal interval #{interval}” if interval \< 0

File.open(file) do |io|  
loop do  
while ( line = io.gets )  
puts line  
end

```
   # uncomment next to watch what is happening
   # puts "-"
   sleep interval
 end

```

end  
end

Cheers

robert
