开发者

Why is this Perl script using the File::Tail module not working?

I'm working on a simple Perl script to monitor a log file using the File::Tail module, but I can't seem to get the module working properly.

The idea is to use it over IRC, but this didn't seem to work so after tinkering with the interactive interpreter I've narrowed the problem down to File::Tail. I've cut it down the follo开发者_Python百科wing basic example to monitor the file and nothing happens at all when new entries are appended to the file:

#!/usr/bin/perl -w

use strict;

use File::Tail;

my $file = File::Tail->new("/var/log/apache2/error.log");

while(defined(my $line = $file->read))
{
    print "$line\n";
}

Can anyone suggest what the problem might be? I've gone through the perldoc entry and this is virtually copied from there so I can't really see that I've made any glaring errors. I'm running Ubuntu Lucid.


Is it possible that this is a permissions error? Can the user you're running the script as access /var/log/apache2/error.log?


If all else fails, implement it yourself!

use Fcntl qw(:seek);

while (1) {
    while (<$fh>) {
        ...
    }
    sleep(1);
    seek $fh, 0, SEEK_CUR;
}

...Which I wouldn't have had to type if I had simply linked you here or here.


I've just realised that I've been a complete tool...

It works perfectly - I just didn't leave it long enough before hitting Ctrl-C. D'oh! Oh well, lesson learned! Thanks for your help anyway!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜