代码之家  ›  专栏  ›  技术社区  ›  Shantesh

Perl File::Monitor模块为创建的新文件通知多个通知,而我期望一个通知

  •  1
  • Shantesh  · 技术社区  · 9 年前

    下面是代码片段!我使用Perl模块file::Monitor进行文件创建通知。

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    use File::Monitor;
    use File::Basename;
    use Time::HiRes qw {usleep};
    
    my $pid,@pids;
    
    
    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 '.txt' extension is
                                                             # not found, otherwise it's '.txt'.
        if ($ext eq '.log') {
            print "$path was created\n";
        #-----------------------------------------Forking part #-----------------------------------------
        defined ($pid = fork()) or die "Couldn't fork: $!";
        if ($pid == 0) { #then in child process
            print "Loop got executed\n";
        }
        else {  #then in parent process, where $pid is the pid of the child
            push @pids, $pid;
        }
        #-----------------------------------------Forking part ends here #-----------------------------------------
        }
    }
    }
    
    my $monitor = File::Monitor->new();
    
    $monitor->watch( {
    name        => '/home/goudarsh/Desktop/logs/',
    recurse     => 1,
    callback    => {files_created => \&textfile_notifier},  #event => handler 1 
    } );
    
    $monitor->scan;
    while (1) {
    $monitor->scan; #Scanning the directory one
    #sleep(2);
    #usleep(10_000); #$microseconds = 750_000;
    for my $pid (@pids) {
        waitpid($pid, 0)  #0 => block
    }
    }
    

    文件创建

    =============================

    touch 1.log  
    touch 2.log  
    touch 3.log  
    touch 4.log  
    

    脚本的输出

    =========================

    /home/goudarsh/Desktop/logs/1.log was created  
    /home/goudarsh/Desktop/logs/2.log was created  
    /home/goudarsh/Desktop/logs/2.log was created  
    /home/goudarsh/Desktop/logs/3.log was created  
    /home/goudarsh/Desktop/logs/3.log was created  
    /home/goudarsh/Desktop/logs/3.log was created  
    /home/goudarsh/Desktop/logs/4.log was created  
    /home/goudarsh/Desktop/logs/3.log was created  
    /home/goudarsh/Desktop/logs/4.log was created  
    /home/goudarsh/Desktop/logs/4.log was created  
    /home/goudarsh/Desktop/logs/4.log was created  
    /home/goudarsh/Desktop/logs/4.log was created  
    /home/goudarsh/Desktop/logs/4.log was created  
    /home/goudarsh/Desktop/logs/4.log was created  
    /home/goudarsh/Desktop/logs/4.log was created      
    

    正如我所期望的那样,在终端上为一个文件打印一个通知/警报。 我怀疑的是“分叉部分”部分有一些bug!

    你知道我哪里做错了吗?

    1 回复  |  直到 9 年前
        1
  •  0
  •   Shantesh    9 年前

    这就是解决我问题的方法

    use strict;
    use warnings;
    use File::Monitor;
    use File::Basename;
    use Time::HiRes qw {usleep};
    my $script = '/homw/goudarsh/Desktop/script.pl';
    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 '.txt' extension is
                                                         # not found, otherwise it's '.txt'.
    if ($ext eq '.log') {
        print "$path was created\n";
        system("perl $script $path \&");
    }
    }}
    my $monitor = File::Monitor->new();
    $monitor->watch( {
    name        => '/home/goudarsh/Desktop/logs/',
    recurse     => 1,
    callback    => {files_created => \&textfile_notifier},  #event => handler 1 
    } );
    
    $monitor->scan;
    while (1) {
    $monitor->scan; #Scanning the directory one
    sleep(2);
    #usleep(10_000); #$microseconds = 750_000;
    
    }
    

    我只想执行 $script 具有 $path 作为一个论点,我使用forking创建子进程,但我对使用fork的概念是错误的,这是不需要的!现在不用分叉,我可以做任何需要的事情,我很高兴能学到新东西