简体   繁体   English

在PID中用perl尾部文件

[英]tail a file in perl with PID

I have a requirement to check a log file till a PID is alive in perl. 我需要检查日志文件,直到在Perl中PID仍然存在为止。 It is working fine in bash. 它在bash中工作正常。

tail -f --pid=1234 logfile.log

Now I need the same to do with perl to check a log file either by checking the PID or giving the time interval also works for me. 现在,我需要通过检查PID或给定时间间隔来对perl进行检查日志文件,这同样适用于我。

I am using the following code to do it in perl, but I am able to tail a file and have two issues in it. 我正在Perl中使用以下代码来执行此操作,但是我能够拖尾一个文件并且其中有两个问题。

1) Tail of a file is not happening immediatly. 1)文件尾没有立即发生。

2) I want to close the tail after few seconds, say 100. 2)我想在几秒钟后闭合尾巴,例如100。

use warnings;
use strict;
use File::Tail; 
my $name='/tmp/out'; 
my $file=File::Tail->new(name=>$name,interval=>1,maxinterval=>5,adjustafter=>1);
my ($found,$timeleft,@pending) = File::Tail::select(undef,undef,undef,100,$file);

if ($found)
{
   print $pending[0]->read;
}

I am using the above code and it is exiting in only 2 seconds. 我正在使用上面的代码,它仅在2秒钟内退出。 I am looking for to tail a file like exactly tailf command in linux to exit after the time given. 我正在寻找在给定时间后像Linux中的tailf命令一样拖尾文件以退出。

Thanks. 谢谢。

From the documentation : 文档中

The module tries very hard NOT to "busy-wait" on a file that has little traffic. 该模块非常努力地不对流量很少的文件“忙碌等待”。 Any time it reads new data from the file, it counts the number of new lines, and divides that number by the time that passed since data were last written to the file before that. 每当它从文件中读取新数据时,它都会计算新行数,并将该数除以自从数据最后一次写入文件之前经过的时间。 That is considered the average time before new data will be written. 这被认为是新数据写入之前的平均时间。 When there is no new data to read, File::Tail sleeps for that number of seconds. 当没有新的数据要读取时,File :: Tail睡眠该秒数。 Thereafter, the waiting time is recomputed dynamicaly. 此后,动态重新计算等待时间。 Note that File::Tail never sleeps for more than the number of seconds set by maxinterval. 请注意,File :: Tail的休眠时间不会超过maxinterval设置的秒数。

So you need to set maxinterval to a low number if you want it to see updates immediately. 因此,如果您希望maxinterval立即查看更新,则需要将其设置为一个maxinterval的数字。

Update: to timeout, use select . 更新:要超时,请使用select This simple example does a single, 100-second check on the file. 这个简单的示例对文件进行了一次100秒的检查。 It also checks the file at least every five seconds. 它还至少每五秒钟检查一次文件。 For a more complex example using a loop, see the aforementioned documentation. 有关使用循环的更复杂示例,请参见上述文档。

use warnings;
use strict;
use File::Tail; 
my $name='/tmp/out'; 
my $file=File::Tail->new(name=>$name,interval=>1,maxinterval=>5,adjustafter=>1);
my ($found,$timeleft,@pending) = File::Tail::select(undef,undef,undef,100,$file);

if ($found)
{
   print $pending[0]->read;
}

Update: here is a looping solution that will keep going for your entire time interval: 更新:这是一个循环解决方案,将在您的整个时间间隔内持续进行:

use warnings;
use strict;
use File::Tail; 
my $name='/tmp/out'; 
my $file=File::Tail->new(name=>$name,interval=>1,maxinterval=>5,adjustafter=>1);

my $time = 60;

while ($time > 0)
{
    my ($found,@pending);
    ($found,$time,@pending) = File::Tail::select(undef,undef,undef,$time,$file);

    if ($found)
    {
        print $_->read for @pending;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM