This is how I am doing it right now but it locks the file.
#!/usr/bin/perl
use Env qw( $USERNAME );
use File::Tail;
use strict;
use warnings;
my $file = $ARGV[0];
print "$file\n";
my $fileTail = File::Tail->new( name=>$file, maxinterval=>5, tail=>-1);
my $line;
while ( defined( $line = $fileTail->read ) )
{
print $line;
}
exit;
According to the documentation, it shouldn't be locking. What is your operating system though? I wonder if you are using Windows, although the shebang line suggests not. Some more details on your environment would therefore be helpful.
Windows (or NTFS... or how Perl implements open on Windows... not exactly sure) has mandatory locking rolled into open(). If you open a file for reading, others will not be able to open it for writing. If you open a file for writing, others will not be able to open it for reading or writing.
You're holding the file open for reading, so nobody can write to the log. I think that's what's happening. File::Tail probably doesn't account for this. It's working at all because File::Tail seems to close and reopen the filehandle from time to time if it doesn't see any activity, it assumes it's been truncated or recreated. This releases your lock and lets other files slip in to write.
You can test this by opening a file for reading with one Perl process, and then try to open it for appending by another.
I believe one way to deal with this is to open the log file using Windows specific functions that allow you to control the locking behavior. Win32::SharedFileOpen seems to be the thing.
fsopen(my $fh, $file, 'r', SH_DENYNO) or
die "Can't read '$file' with no locks: $!\n";
That will open a file for reading with no locks. Unfortunately, you're responsible for doing the rest of the work. The perlfaq might help.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With