我实际上是在监视目录以创建新文件(.log文件)这些文件是由某些工具生成的,并且在创建同一文件的某个时间之后写入日志条目,在此期间文件将为空。 / p>
我怎么能等到有东西写入日志并且原因是基于日志条目我将调用不同的脚本!,
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;
}
基本上我正在从日志中获取一些重要信息。
答案 0 :(得分:3)
对于这样的问题表述,这样的事情可能会对你有所帮助:
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
}
了解更多here
答案 1 :(得分:0)
您只监视日志文件的创建。也许你可以在回调子中使用sleep函数来等待写入的日志文件。您也可以监视文件更改,因为某些日志文件可以扩展。