2012-05-04 2 views
0

파일을보기 위해 아래의 테스트 코드를 실행하면 파일에 'vim'을 입력하면 이벤트가 감지됩니다. 또는 쓰기 종료). 파일에 'echo'를하거나 perl을 통해 텍스트를 추가하는 것은 감지되지 않습니다.Perl Inotify가 append에 응답하지 않음 (즉, echo 'test'>> 파일)

test_inotify.pl :

#!/usr/bin/perl 

use strict; 
use warnings; 
use diagnostics; 
use Carp; 
use IO::File; 
use Linux::Inotify2; 

$|++; 

my $readfile   = shift; 
#my $action  = "IN_CLOSE_WAIT"; 
#my $action  = "IN_MODIFY"; 
#my $action  = "IN_OPEN"; 
my $action  = "IN_ALL_EVENTS"; 

unless ($readfile) { $readfile = "test.txt" }; 

my $inotify  = Linux::Inotify2->new(); 

$inotify->watch($readfile, $action) 
       or die "Inotify watch on " . $readfile . "failed: $!\n"; 

while() { 

    my @events = $inotify->read(); 

    unless (@events > 0) { 
     print "Inotify Read Error: $!\n"; 
     exit; 
    }; 

    foreach my $event (@events) { 
     print "Detected Event: " . $event->fullname . "\n"; 
    }; 

}; 

test_fh_write.pl : 나는 에코뿐만 아니라 test_fh_write.pl으로 시도했습니다

#!/usr/bin/perl -w 

use strict; 
use warnings; 
use diagnostics; 
use Carp; 
use IO::File; 

$|++; 

my $readfile   = shift; 

unless ($readfile) { $readfile = "test.txt" }; 

my $readfh    = IO::File->new($readfile, ">>") or 
#my $readfh    = IO::File->new($readfile, ">") or 
           die "Cannot open $readfile: $!"; 

$readfh->autoflush(1); 

if ($readfh) { 

    print $readfh "test\n\n"; 

}; 

undef($readfh); 

은 다음과 같이 명령 : '에코 >> TEST.TXT을 ','echo "test">> test.txt '등

"$ |" ($ fh-> autoflush (1)을 사용해도), 아무 소용이 없습니다. test_inotify.pl에 정의 된 $ action 변수 각각은 시도했지만 모두 똑같습니다.

답변

4

Linux::Inotify2::watch의 두 번째 인수는 문자열이 아니라 숫자/비트 마스크입니다. 당신은 (아마도 상수) 기능 &Linux::Inotify2::IN_ALL_EVENTS에 해결

$inotify->watch($readfile, IN_ALL_EVENTS) 

대신

$inotify->watch($readfile, "IN_ALL_EVENTS") 

bareword는 IN_ALL_EVENTS

를 호출해야합니다.

+0

감사합니다. 희망적으로 이것은 다른 사람을 돕는다. – xyon