Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Linux::Inotify2 - unable to receive events

by jfroebe (Vicar)
on Oct 03, 2008 at 21:36 UTC ( #715286=perlquestion: print w/ replies, xml ) Need Help??
jfroebe has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

I'm trying to monitor a directory "/tmp/j" with Linux::Inotify2 to be notified when a file is accessed or when a (write) file descriptor is closed. So far, I haven't been able to receive any notification events :( My guess is that I'm missing something simple.

#!/usr/bin/perl use strict; use warnings; use Linux::Inotify2; $|++; my $inotify = new Linux::Inotify2 or die "Unable to create new inotify object: $!"; 1 while $inotify->poll; $inotify->watch("/tmp/j", IN_ACCESS | IN_CLOSE_WRITE, sub { my $event = shift; my $name = $event->fullname; print "$name was accessed\n" if $event->IN_ACCESS; print "$name is no longer mounted\n" if $event->IN_UNMOUNT; print "$name is gone\n" if $event->IN_IGNORED; print "$name is new\n" if $event->IN_CLOSE_WRITE; print "events for $name have been lost\n" if $event->IN_Q_OVERFLOW; print "jjjj\n"; $event->w->cancel; }) or die "watch creation failed: $!";

$ dd if=/dev/zero of=/tmp/j/p bs=1M count=2
2+0 records in
2+0 records out
2097152 bytes (2.1 MB) copied, 0.101007 s, 20.8 MB/s

The script runs with no warnings.

linux kernel 2.6.24-19-generic

update:

To rule out an issue with the os, I tested with the inotifywatch of the inotify tools package. Running the dd command above several times.

$ inotifywatch -v -e access -e modify -t 20 -r /tmp/j Establishing watches... Setting up watch(es) on /tmp/j OK, /tmp/j is now being watched. Total of 1 watches. Finished establishing watches, now collecting statistics. Will listen for events for 20 seconds. total modify filename 18 18 /tmp/j/

Update to the update:

Did more testing. Looks like the perldoc is wrong. The $inotify->poll needs to be called AFTER the $inotify->watch routines are set!

Thanks Animator for making me rethink how I was calling it. Working code:

#!/usr/bin/perl use strict; use warnings; use Linux::Inotify2; $|++; my $inotify = new Linux::Inotify2 or die "Unable to create new inotify object: $!"; $inotify->watch("/tmp/j", IN_ACCESS | IN_CLOSE_WRITE, sub { my $event = shift; my $name = $event->fullname; print "$name was accessed\n" if $event->IN_ACCESS; print "$name is no longer mounted\n" if $event->IN_UNMOUNT; print "$name is gone\n" if $event->IN_IGNORED; print "$name is new\n" if $event->IN_CLOSE_WRITE; print "events for $name have been lost\n" if $event->IN_Q_OVERFLOW; print "jjjj\n"; # $event->w->cancel; }) or die "watch creation failed: $!"; 1 while $inotify->poll;

Jason L. Froebe

Blog, Tech Blog

Comment on Linux::Inotify2 - unable to receive events
Select or Download Code
Re: Linux::Inotify2 - unable to receive events
by Animator (Hermit) on Oct 03, 2008 at 22:01 UTC

    I'm facing some problems installing Linux::Inotify2 so I can't test it myself at the moment but this is what I noticed:

    The SYNOPSIS of Linux::Inotify2 contains Event->... your code does not.

    Did you try the examples in the Linux::Inotify2 package? eg/simple and eg/event? Did these work?

      I think you're referring to the following snippet:

      # for Event: Event->io (fd =>$inotify->fileno, poll => 'r', cb => sub { $inotify-> +poll }); # for Glib: add_watch Glib::IO $inotify->fileno, in => sub { $inotify->poll }; # manually: 1 while $inotify->poll;

      These are three separate ways of creating a loop for $inotify->poll. Yup, I tried all three ways. I'm using the third method to isolate the issue.

      Thanks though

      Jason L. Froebe

      Blog, Tech Blog

      Did more testing. Looks like the perldoc is wrong. The $inotify->poll needs to be called AFTER the $inotify->watch routines are set!</p.

      Thanks Animator for making me rethink how I was calling it.

      Jason L. Froebe

      Blog, Tech Blog

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://715286]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (14)
As of 2014-08-27 14:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (239 votes), past polls