So I'm looking to create some utilities to watch various logs/etc and perform actions when certain criteria is found in the files. I have a basic example working, but if the log is truncated a certain way, the script doesn't detect the file change. Any ideas?
Code:
cat /dev/null > logfile
After something like that it doesn't catch the first addition.
I also ran across the
File::Tail module, but would rather have all the code self contained instead of having external requirements..
Code:
use File::stat;
use IO::Handle;
$| = 1;
$file = '/tmp/foo1';
open(FH, $file) or die "$!\n";
for (;;) {
while(<FH>) { print }
sleep 1;
if (stat(*FH)->nlink == 0) {
print "=== new FH\n";
close(FH);
open(FH, $file) or die "$!\n";
sleep 5;
}
else {
seek(FH, 0, 1);
}
# print "=== end for pos=" . tell(FH) . "\n";
}
close(FH);
I was also planning on watching the inode and filesize (for shrinking) and if they change, to close the FH and reopen. Any cool ideas or should I just give up and use the module?
-r