Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

How do I tail -f a file if it is NFS mounted?

by smart_amorist (Initiate)
on Jan 23, 2009 at 13:03 UTC ( #738470=perlquestion: print w/replies, xml ) Need Help??

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

Hi All, I am writing a code to monitor running processes and their log files. Suppose I have two Processes: Process_1 and Process_2, logs are Log_1 and Log_2. Everyday process, run it write to same log file. I want to run the program in loop. First run it should point to end of current log. It will finish its first run after looking for Log_1 and Log_2. In next run it (say sleeping for 5 mins) shoud only read the newly added lines in log Log_1 , Log_2 and check for exceptions. It should not read the log file from beginning during each cycle. i am trying the above code, but it is not working. I am new to perl, so can someone write down a small exact piece of code.
  • Comment on How do I tail -f a file if it is NFS mounted?

Replies are listed 'Best First'.
Re: How do I tail -f a file if it is NFS mounted?
by targetsmart (Curate) on Jan 23, 2009 at 14:54 UTC
    'perldoc -q tail' might help you, if no perldoc installed here is from perlfaq5
    How do I do a "tail -f" in perl? First try seek(GWFILE, 0, 1); The statement "seek(GWFILE, 0, 1)" doesn't change the current p +osition, but it does clear the end-of- file condition on the handle, so that the next <GWFILE> makes P +erl try again to read something. If that doesn't work (it relies on features of your stdio imple +mentation), then you need something more like this: for (;;) { for ($curpos = tell(GWFILE); <GWFILE>; $curpos = tell +(GWFILE)) { # search for some stuff and put it into files } # sleep for a while seek(GWFILE, $curpos, 0); # seek to where we had bee +n } If this still doesn't work, look into the POSIX module. POSIX +defines the clearerr() method, which can remove the end of file condition on a filehandle. The meth +od: read until end of file, clearerr(), read some more. Lather, rinse, repeat. There's also a File::Tail module from CPAN.

    but the data transfer over NFS will not be live, there would be minimal delay if you expect live data.
Re: How do I tail -f a file if it is NFS mounted?
by cdarke (Prior) on Jan 23, 2009 at 13:41 UTC
    i am trying the above code, but it is not working

    Maybe I missed it, but I can't see any code to comment on.

    it is not working

    What happens?

    There can be an issue with buffering and NFS. I suggest that you get your program working locally first and then try it on NFS.
Re: How do I tail -f a file if it is NFS mounted?
by matija (Priest) on Jan 23, 2009 at 14:45 UTC

    You can try using File::Tail but be warned: it can become unpredictable over NFS if the two machines aren't showing the same time (i.e. they should be synnchronized through NTP or something)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2020-07-02 06:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?