Search code examples
macosbashlsofafp

Bash on OSX: How to determine if network file (AFP) is in use?


I've got a bash script that runs on OSX.
It needs to manipulate some files on a network-share (AFP share on a Synology NAS).

Unfortunately those files are sometimes still being written when the script runs.

How do I determine if the file is in use or not ?

The normal method is by using "lsof", but that doesn't seem to work on network files if the other user is coming from another client on the LAN.

I could just attempt to rename the file. I suppose that will fail if the file is in use, but that is far from elegant.

Anybody have a better solution ?


Solution

  • This is not a generally solvable problem. The typical solution is to write the file to a temporary location and then move it to the final processing directory (since move within a filesystem is generally atomic). If you cannot control how or where the file is written, then you are left with heuristics, particularly doing things like looking at the file and seeing if it hasn't grown in "awhile," but none of these are particularly good compared to separating the writing from the enqueuing.