http://www.perlmonks.org?node_id=440351


in reply to How to detect if file is in use?

What I did in the past was renaming the file. If you can rename it, it is not in use.

Particularly, when a process might reopen the file and append something, you have the advantage of having your file to work with and the process starts to write a new file.

Update:
Gellyfish is right, I did it only with MS/IBM/DEC-(nonUNIX) Systems.

Replies are listed 'Best First'.
Re^2: How to detect if file is in use?
by gellyfish (Monsignor) on Mar 17, 2005 at 12:32 UTC

    Nope that is not true in the general sense, which can be demonstrated simply by running:

    open FOO, ">x"; $foo = <>;
    in one window and then in another:
    [jonathan@orpheus test]$ /sbin/fuser -u x x: 5665(jonathan) [jonathan@orpheus test]$ mv x x1 [jonathan@orpheus test]$ /sbin/fuser -u x1 x1: 5665(jonathan)
    However it is true for most Windows applications.

    /J\

Re^2: How to detect if file is in use?
by bart (Canon) on Mar 17, 2005 at 13:57 UTC
    You can rename, or even delete a file while it is open, on Linux, *BSD, and similar OSes. That's one way to create a temporary anonymous file, which will disappear as soon as your open handle to it closes. It is caused by the strong dissociation between file name and file contents. Hard links are another example, where you can have two independent and completely fully qualified (no limits) paths to the same file contents.

    Also, if on those platforms, you attempted to rename a log file for for example Apache while it is running, you'll see it doesn't stop it from still growing.

    Windows won't allow you to rename or delete the file while its open, that's true.