Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Tailing a log file using seek/tell

by atistler (Acolyte)
on Mar 16, 2007 at 22:41 UTC ( [id://605228]=perlquestion: print w/replies, xml ) Need Help??

atistler has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to tail a file. I used the example from perlfaq5 and it works just fine if I tail a file in my /tmp/ dir. However if I move that same file to any other dir, the script just reads to the end of the file. If I write a little more to that file, then save, the script will not output it. I am assuming this is because eof cannot be reset for some reason. Any ideas?? I think this should be reproducible.
open (GWFILE, "/tmp/logfile"); #open (GWFILE, "/opt/logfile"); for (;;) { my $curpos; for ($curpos = tell(GWFILE); <GWFILE>; $curpos = tell(GWFILE)) { print; } # sleep for a while sleep 1; seek(GWFILE, $curpos, 0); # seek to where we had been }

Replies are listed 'Best First'.
Re: Tailing a log file using seek/tell
by ikegami (Patriarch) on Mar 16, 2007 at 23:26 UTC
Re: Tailing a log file using seek/tell
by kunwon1 (Sexton) on Mar 16, 2007 at 23:05 UTC

    I tried your example code, and I got the same behavior from it irregardless of the location of the file. What I did notice is that any time I added anything more than two lines long to the file, the program would show only the last two lines, and then stop tailing.

    So, I'd guess your issue is something else entirely.

Re: Tailing a log file using seek/tell
by rinceWind (Monsignor) on Mar 17, 2007 at 13:31 UTC

    On some operating systems, you need to actually close and reopen the file in order for it to see that the EOF has moved. You can use stat while the file is closed, to see whether the file has grown, hence whether it's worth opening it and reading the next chunk.

    Hope ths helps.

    --
    Apprentice wetware hacker

Re: Tailing a log file using seek/tell
by atistler (Acolyte) on Mar 19, 2007 at 07:00 UTC
    Thanks for the help. I knew about Tail::File. I looking to do this on my own, without a module. What is the reason that clearerr does not reset EOF on certain OS's? I am using rh3 by the way. I just don't get why this works in tmp and not in other directories. I know that I can reopen the filehandle each time, seek to the same place where I was in the file and read from there. Is opening a FH an expensive process in comparision to clearing EOF. I would think so. I wanted to keep this as minimalistic as possible. Going to test further, will let you guys know what I find.
      Oh yeah, this is a rotating log file by the way. So I have to take that into consideration.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://605228]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2024-04-19 10:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found