Regex 我怎么能等到perl脚本中有东西写入日志文件
实际上,我正在监视一个目录以创建新文件(.log files)。这些文件是由一些工具生成的,在创建同一文件后,工具会写入日志条目,在此期间,文件将为空 我怎么能等到有东西被写入日志并根据我将调用的不同脚本的日志条目进行推理Regex 我怎么能等到perl脚本中有东西写入日志文件,regex,perl,file,wait,monitor,Regex,Perl,File,Wait,Monitor,实际上,我正在监视一个目录以创建新文件(.log files)。这些文件是由一些工具生成的,在创建同一文件后,工具会写入日志条目,在此期间,文件将为空 我怎么能等到有东西被写入日志并根据我将调用的不同脚本的日志条目进行推理 use strict; use warnings; use File::Monitor; use File::Basename; my $script1 = "~/Desktop/parser1.pl"; my $scrip2t = "~/Desktop/parser2.pl
use strict;
use warnings;
use File::Monitor;
use File::Basename;
my $script1 = "~/Desktop/parser1.pl";
my $scrip2t = "~/Desktop/parser2.pl";
my $dir = "~/Desktop/tool/logs";
sub textfile_notifier {
my ($watch_name, $event, $change) = @_;
my @new_file_paths = $change->files_created; #The change object has a property called files_created,
#which contains the names of any new files
for my $path (@new_file_paths) {
my ($base, $fname, $ext) = fileparse($path, '.log'); # $ext is "" if the '.log' extension is
# not found, otherwise it's '.log'.
if ($ext eq '.log') {
print "$path was created\n";
if(-z $path){
# i need to wait until something is written to log
}else{
my @arrr = `head -30 $path`;
foreach(@arr){
if(/Tool1/){
system("/usr/bin/perl $script1 $path \&");
}elsif(/Tool1/){
system("/usr/bin/perl $script2 $path \&");
}
}
}
}
my $monitor = File::Monitor->new();
$monitor->watch( {
name => $dir,
recurse => 1,
callback => {files_created => \&textfile_notifier}, #event => handler
} );
$monitor->scan;
while(1){
$monitor->scan;
}
基本上,我正在从日志中删除一些重要信息。您只监视日志文件的创建。也许您可以在callback sub中使用sleep函数来等待日志文件被写入。您也可以监视文件更改,因为某些日志文件可以扩展。您只监视日志文件的创建。也许您可以在callback sub中使用sleep函数来等待日志文件被写入。您也可以监视文件更改,因为某些日志文件可以扩展。对于您问题的这种表述,类似这样的内容可能会帮助您:
use File::Tail;
# for log file $logname
my @logdata;
my $file = File::Tail->new(name => $logname, maxinterval => 1);
while (defined(my $newline = $file->read)) {
push @logdata, $newline;
# the decision to launch the script according to data in @logdata
}
阅读更多关于你问题的表述,类似这样的内容可能会对你有所帮助:
use File::Tail;
# for log file $logname
my @logdata;
my $file = File::Tail->new(name => $logname, maxinterval => 1);
while (defined(my $newline = $file->read)) {
push @logdata, $newline;
# the decision to launch the script according to data in @logdata
}
阅读更多您是否阅读了有关轮询文件句柄的内容?@red0ct否!链接到这些信息会很有帮助。这可能是一个合理的选择吗?@Eduardoballestors谢谢,我现在正在尝试!!你读过关于轮询文件句柄的内容吗?@red0ct没有!链接到这些信息会很有帮助。这可能是一个合理的选择吗?@Eduardoballestors谢谢,我现在正在尝试!!