Search code examples
perlmonitorinotifyinode

Can we use Perl 'inotify2.pm' perl module for monitoring file on the shared directories having different machines?


my intention is to monitor for newly created files in a directory and the same directory is shared among many computers and below is the script for your reference and once i run the script which identifies the files which are created from the machine xyz (on which i am running notifier script)and it does not recognizes the files from machine abc! Is there anything i need to consider for monitoring newly created files under the given directory ?. irrespective of the machine from which the files are created.

Is there any way to track the file created on different machines using 'inotify2.pm' ?

use Linux::Inotify2;
my $inotify;
$inotify = new Linux::Inotify2 ;
my $dir="/home/vikas/";
opendir(DIR, $dir);

while(readdir DIR)
{
   print "$dir/$_\n";
  -d $_ and $inotify->watch($_,IN_CREATE, \&watch_new);
}
close DIR;
sub watch_new
{
  my $e = shift;
  my $name = $e->fullname;
  print "File was created! $name\n";
}
while(1)
{
  $inotify->poll;
}

Solution

  • No, you can't. NFS doesn't work this way.

    inotify is a kernel hook, that lets the kernel notify 'watchers' when 'something else' changes the file.

    However the kernel isn't made aware of every change on a remote filesystem, because that'd get a bit insane, fast. So it can only 'notify' for changes that go through the kernel, and practically speaking that means only stuff that happens locally. (Which can, in some cases, be remote filesystems, but the notification doesn't propagate)

    If you need to do this, then the answer is really - don't use a filesystem as a semaphore mechanism. It's optimised for 'at rest' data, not real time synchronisation between distributed processes. Use a message passing system instead. I've used Redis with Publish/Subscribe for this sort of IPC, and it works very nicely, for example.