2

As per solution provided in perldoc, I am trying to emulate tail -f but it's not working as expected. The below code could print all lines first time but not the newly lines appended. Could you please elaborate if I am missing any thing here.

#!/usr/bin/perl
open (LOGFILE, "aa") or die "could not open file reason $! \n";

for (;;)
{
        seek(LOGFILE,0,1);  ### clear OF condition        
        for ($curpos = tell(LOGFILE); <LOGFILE>; $curpos = tell(LOGFILE)) 
        {
                print "$_ \n";
        }

        sleep 1;
        seek(LOGFILE,$curpos,0); ### Setting cursor at the EOF
}
2
  • aa is the file name on which is continuously growing and I need to do tail -f aa.
    – user419534
    May 4, 2012 at 2:40
  • 3
    See also File::Tail
    – ikegami
    May 4, 2012 at 3:08

1 Answer 1

1

works fine for me. How are you updating "aa" ? You wont see the data immediately if it is a buffered write to "aa". can you try the following in a different terminal and check whether you are seeing any update.

while ( 1 )
echo "test" >> aa
end

If you are using perl to update aa, check this section on buffering and how to disable.

5
  • If I add lines using vi editor it is not working. while echo "test" >> aa does work.
    – user419534
    May 4, 2012 at 2:57
  • 2
    then you are probably suffering from buffering.
    – mob
    May 4, 2012 at 3:42
  • what about tail -f aa when you use vi ? Are you seeing different behaviour with tail -f "aa" and your script when you use vi to update "aa" ?
    – dpp
    May 4, 2012 at 5:08
  • 7
    When editing vi probably doesn't write to the same file. I'd guess it writes to a temporary file and then deletes the old one and renames the temporary one. Because your script has the old file still open the old file will remain (but not appear in directory listings). If you want to detect file replacement then you'll want to loop running "stat" on the file and look for changes in size/timestamps. Then open it, read whatever you want and close the file again. May 4, 2012 at 6:44
  • Thanks Richard for the explaining the anomaly.
    – user419534
    May 6, 2012 at 11:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.