[英]How to tail file in perl when copytruncate is used
我创建了一个简单的perl脚本来读取日志文件并异步处理数据。
reading子节点还检查inode编号的变化,以便在日志旋转时创建新的文件句柄。
我面临的问题是,在logrotation中使用copytruncate
时,旋转文件时inode不会改变。
这不应该是一个问题,因为脚本应该继续读取文件,但是由于某些我无法立即看到的原因,一旦日志轮转,就不会再读取新行了。
我如何修改以下脚本(或完全废弃并重新启动),以连续copytruncate
使用perl使用copytruncate
进行logrotated的文件?
use strict;
use warnings;
use threads;
use Thread::Queue;
use threads::shared;
my $logq = Thread::Queue->new();
my %Servers :shared;
my %servername :shared;
#########
#This sub just reads the data off the queue and processes it, i have
#reduced it to a simple print statement for simplicity.
#The sleep is to prevent it from eating cpu.
########
sub process_data
{
while(sleep(5)){
if ($logq->pending())
{
while($logq->pending() > 0){
my $data = $logq->dequeue();
print "Data:$data\n";
}
}
}
}
sub read_file
{
my $myFile=$_[0];
#Get the argument and assign to var.
open(my $logfile,'<',$myFile) || die "error";
#open file
my $Inode=(stat($logfile))[1];
#Get the current inode
seek $logfile, 0, 2;
#Go to the end of the file
for (;;) {
while (<$logfile>) {
chomp( $_ );
$logq->enqueue( $_ );
#Add lines to queue for processing
}
sleep 5;
if($Inode != (stat($myFile))[1]){
close($logfile);
while (! -e $myFile){
sleep 2;
}
open($logfile,'<',$myFile) || die "error";
$Inode=(stat($logfile))[1];
}
#Above checks if the inode has changed and the file exists still
seek $logfile, 0, 1;
#Remove eof
}
}
my $thr1 = threads->create(\&read_file,"test");
my $thr4 = threads->create(\&process_data);
$thr1->join();
$thr4->join();
#Creating the threads, can add more log files for processing or multiple processing sections.
logrotate的日志配置包含
compress
compresscmd /usr/bin/bzip2
uncompresscmd /usr/bin/bunzip2
daily
rotate 5
notifempty
missingok
copytruncate
该文件。
眼镜
GNU bash, version 3.2.57(1)-release (s390x-ibm-linux-gnu)
perl, v5.10.0
(if logrotate has version and someone knows how to check then i will also add that)
需要更多信息只是问。
因此,当您查看copytruncate
,失败的原因非常明显,它会复制原始文件,然后截断当前文件。
尽管这确保保留了索引节点,但它又产生了另一个问题。
按照我当前尾部处理文件的方式,只需停留在末尾并删除eof标志,这意味着当文件被截断时,指针停留在截断前的最后一行的位置,这又意味着不再有行将被读取,直到再次到达该指针。
显而易见的解决方案是简单地检查文件的大小,并在指针指向文件末尾时重置指针。
我发现使用下面的两行代码来检查文件大小是否变得更容易。
my $fileSize=(stat($logfile))[7];
#Added after the inode is assigned
和改变
if($Inode != (stat($myFile))[1]){
至
if($Inode != (stat($myFile))[1] || (stat($myFile))[7] < $fileSize){
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.