当运行下面的测试代码来观察文件时,只有当我对文件执行“vim”并将其写出(或写退出)时,才会检测到事件。未检测到对文件执行“回显”或通过 perl 添加文本。
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:
#!/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_fh_write.pl 以及像这样的 echo 命令:'echo a >> test.txt'、'echo "test" >> test.txt' 等。
我也试过用“$|” 字符和没有(即使使用 $fh->autoflush(1)),但无济于事。我尝试过在 test_inotify.pl 中定义的每个 $action 变量,但它们的结果都是一样的。