NFS - Detecting remotely created files programmatically?

I'm trying to monitor an NFS share and detect the creation of a file by a process running on a remote system. The problem is that the detection isn't working after the file has been created, my script still thinks there's no file. If someone performs an ls in the directory, or I run a separate process that just does ls periodically in the background, the script will THEN detect that the file exists. Adding the "ls the directory" feature into the main script doesn't work, though: it still never picks up the existence of the file until an external process causes the directory list contents to be refreshed, somewhere somehow.

Here's a test case that illustrates the problem:

watch_file.pl:

#!/usr/bin/perl -w
my @stat;
while (1) {
    @stat = stat("/nfs/test");
    last if ($stat[1] && $stat[1] > 0);
}

Run that simple script on "host-A":

host-A% ./watch_file.pl &
[1] 9312

Move over to "host-B" and create the file:

host-B% touch /nfs/test

...back over to "host-A", program hasn't exited yet:

host-A% 

...still on "host-A", do an ls in the directory, then the script sees the file:

host-A% ls -a /nfs
.  ..  test
host-A%
[1]+  Done                    ./watch_file.pl &
host-A%

Does anyone know of a simple fix or workaround for this that would be less hokey than running a script in the background to ls the NFS directory every 10 seconds?


By default, most NFS clients cache directory information for 60 seconds.

Mount the NFS share on the client with the noac option to disable attribute caching, or use acdirmin=0,acdirmax=0 to just disable directory caching.