我实际上是监视一个目录以创建新文件(.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的一些重要信息日志。
你看了一些关于轮询的文件句柄? – red0ct
@ red0ct不!这些信息的链接将有所帮助 – Shantesh
可以[File :: Monitor](http://search.cpan.org/~andya/File-Monitor-1.00/lib/File/Monitor.pm)是一个合理的选择吗? – eballes