2016-04-07 13 views
0

我实际上是监视一个目录以创建新文件(.log文件)这些文件是由某些工具和工具生成的,创建相同的文件,在此期间文件将为空。我该如何等待,直到有东西写入我的Perl脚本中的日志文件

,我怎么可以等到的东西被写入日志和原因是基于日志条目,我将调用不同的脚本!

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; 
} 

基本上我从grepping的一些重要信息日志。

+0

你看了一些关于轮询的文件句柄? – red0ct

+0

@ red0ct不!这些信息的链接将有所帮助 – Shantesh

+1

可以[File :: Monitor](http://search.cpan.org/~andya/File-Monitor-1.00/lib/File/Monitor.pm)是一个合理的选择吗? – eballes

回答

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

相关问题