Given the limitations of File::Tail, for such tasks I just
open my $tailfh, "tail -F $logfile |" or die "blunze: $!\n";
tail -F handles re-opening the logfile on inode change and file truncating. To process the gathered lines in chunks, I write them to another file:
my @lines;
while (<$tailfh>) {
push @lines, $_;
unless (-f $lockfile) {
open my $chunkfh, '>>', $chunkfile or die "more blunze: $!\n";
$| = 1; # or use IO::File and autoflush
print $chunkfh @lines;
close $chunkfh;
@lines = ();
}
}
Then another process can touch the lockfile, process the lines in the chunkfile, truncate it and remove the lockfile. The line gathering process then flushes its @lines.
update: if you're wondering what blunze is - that's black pudding (or blood sausage)
update 2: moved the open/close inside the loop, changed the open mode to append.
--shmem
_($_=" "x(1<<5)."?\n".q·/)Oo. G°\ /
/\_¯/(q /
---------------------------- \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
|