How to determine if a file is in use

Rick Stevens ricks at nerd.com
Tue Nov 3 21:52:08 UTC 2009


Donald Russell wrote:
> Another system uses FTP to drop files in a directory for me to process.
> I have a bash script to process the incoming files. The script is started by
> cron periodically.
> 
> There's a problem if the FTP transfer is still in progress because the
> process begins reading the file even though it isn't complete yet.
> 
>>From a bash script, is there a way to tell if the file is still being
> written to?
> I was looking at the lsof command, which will tell me if the file is opened
> or not, so that's a possibility... but it sure seems awkward for the task.
> 
> I could also configure the ftp server to lock files being written, but that
> seems to be discouraged. (based on man vsftpd.conf)
> 
> Basically, what I want is something like
> Can I get an exclusive read on file x?
> No - skip that file, go onto the next one
> Yes - start processing that file
> (I'm not concerned about the possible race condition there... I have other
> protections for that)
> 
> Thanks for any suggestions...

The "lsof(1)" command can tell you if a file is open or in use by some
process, but it is not atomic.

Lock files are useful, but can cause problems if, say, the process that
created the lockfile dies for some reason without removing it.

IIRC, vsftpd creates an exclusive write lock on files that are being
created.  That, or it creates a temp file and when complete, renames it.
Can't recall...it's been awhile since I went trudging through the
source.

You can try to use flock(1) to get locks on files in shells.  See the
man page for it for suggested uses.  I'd suggest using exclusive locks
rather than advisory.
----------------------------------------------------------------------
- Rick Stevens, Systems Engineer                      ricks at nerd.com -
- AIM/Skype: therps2        ICQ: 22643734            Yahoo: origrps2 -
-                                                                    -
-           This message printed using recycled bandwidth            -
----------------------------------------------------------------------




More information about the users mailing list