Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^2: inotify problem

by morgon (Priest)
on Jun 05, 2012 at 22:25 UTC ( [id://974585]=note: print w/replies, xml ) Need Help??


in reply to Re: inotify problem
in thread inotify problem

Thanks for the suggestions, the problem is that when you get the event that the file has moved the copy does not yet exist (preventing a new watch), so you need to watch the directory as well...

Here what seems to work for me, any improvements welcome:

use strict; use Linux::Inotify2; use File::Basename qw(dirname); use File::Spec; $|++; my ($file_to_watch)=@ARGV; my $notify = Linux::Inotify2->new or die $!; &setup_watch; sub setup_watch { $notify->watch ($file_to_watch, IN_MODIFY | IN_MOVE_SELF, \&watch_fi +le) or die $!; } sub watch_file { my($e)=@_; my $fn = $e->fullname; if($e->IN_MODIFY) { print "$fn changed\n"; } if($e->IN_MOVE_SELF) { print "old $fn moved\n"; $e->w->cancel; my $dir = dirname $fn; $notify->watch ($dir, IN_CREATE, \&watch_dir) or die $!; } } sub watch_dir { my($e)=@_; my $fn = File::Spec->canonpath($e->fullname); return unless $fn eq $file_to_watch; $e->w->cancel; print "$fn newly created (probably changed)\n"; setup_watch; } while(1) { my $n = $notify->poll; print "handled $n events\n\n"; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://974585]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2024-03-28 11:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found