8

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?

3
  • Maybe this should be on stackoverflow? Jun 8, 2011 at 17:01
  • Probably right... pardon the noob who is just learning the ins and outs of these great sites. Jun 8, 2011 at 17:45
  • 1
    Not to worry, some moderator will probably move it "real soon now" - it'll still show up here so you can even keep you bookmark :) Jun 8, 2011 at 18:28

1 Answer 1

7

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.

3
  • 1
    Perfection! I'm indebted Hyppy and would upvote you if I had the rep. Jun 8, 2011 at 17:16
  • @Daniel No upvote needed, but an "accept" would be appreciated. (The checkmark under the vote count)
    – Hyppy
    Jun 8, 2011 at 17:21
  • 1
    Done and done. Thanks for being so patient with me! Jun 8, 2011 at 17:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .