7

Scenario:

We are using an NFS share to allow clients to upload raw video material. The files are then polled from the directory on the (NFS) server in order to be processed. Since we do not want to process files which have not finished uploading yet, I want to determine in the server-side script (Bash), if the file is still in use.

As the man pages say, both fuser and lsof -N shall allow for detecting in-use files on NFS shares. However, I do not "see" the files as in use on the server which results in corrupt files later in the processing.

Setup:

My NFS /etc/exports contains these settings (UID and GID being the owner of the shared directory on the server, line wrapped for better display):

/export/foo 10.3.2.0/255.255.255.0 
    (rw,sync,no_subtree_check,all_squash,anonuid=1001,anongid=1001)

The client mounts this share using:

10.3.2.197:/export/foo  /data002/  nfs  defaults        0 0

The server is a Ubuntu 10.04 using "nfs-kernel-server 1:1.2.0-4ubuntu4", the client is an SLES 10 SP2

When I open a file on the client (inside the shared directory), using

echo "Hello" > test.txt && tail -f foo.txt

and then check (still on the client) if the file is in use

fuser foo.txt

I can see that the file is in use. However, when checking on the server side (both fuser foo.txt and lsof -N | grep foo.txt) I do not get any usage information.

How can I check on the server if a file on the NFS share is currently in use (regardless if locally or remote)? Or what am I doing wrong in my current setup?

Axel Knauf
  • 1,600
  • 1
  • 10
  • 12

1 Answers1

6

lsof will only show you which resources are being used by your local system, in no case it'll show any foreign resource acquisition, but there's a way around that...

All remote usage will be tracked by the lockd daemon on the NFS server and it'll issue a lock on the file if you try to access it while being written by another node, so if you're using this (your mount options kinda point that way) you can in that case use lsof to show if a file is locked.

lynxman
  • 9,157
  • 3
  • 24
  • 28
  • lynxman, thanks for your comment. I read about file locks on NFS in another thread[0] in a PHP context. How can this be used from a shell? I saw "flock" when looking at apropos lock, but this is just a function not a command/shell builtin. [0] http://serverfault.com/questions/66919/file-locks-on-an-nfs EDIT: Okay, flock is available in my bash, but it can be used to acquire/release locks on files, not check if there are any. – Axel Knauf Jan 19 '11 at 10:24
  • Well there's no easy way around it, lsof should show you the locking and if it doesn't (which can happen with NFS) just go ahead and write a small program to lock and unlock a file quickly to see if it's locked – lynxman Jan 20 '11 at 08:35
  • 1
    Thanks for your suggestions. I now use a cronjob ob the client, where I can see access to the file(s) using fuser to check if it is in use. If it is not, I move it to another folder (on the share) which is fast. – Axel Knauf Jan 20 '11 at 11:07
  • Glad to see it worked! – lynxman Jan 20 '11 at 11:14