jijogroups has asked for the wisdom of the Perl Monks concerning the following question:

Hi! I have a file which can be accessed through many machines (linux) across a network. I need a way to find out whether a file has been open by some other process, say vi editor. From this forum I've got a solution which can be used only if I'm worried about only one machine see this Fuser under perl Now, I need a solution that I can apply through out the network. Please help!

Edited by planetscape - linkified link

  • Comment on how can i find whether a file has been opened by some other process across a network(not in a single machine)?

Replies are listed 'Best First'.
Re: how can i find whether a file has been opened by some other process across a network(not in a single machine)?
by PreferredUserName (Pilgrim) on Dec 05, 2005 at 14:51 UTC
    > I need a way to find out whether a file has been open by some other process on any machine

    In general, you can't. You'll have to do something cooperative, like have every program agree to use a lock file.

    It might be sufficient just to wrap accesses to the file with something like this:

    # file: vilock perl -MFile::NFSLock -e ' $fname = shift; $lock = File::NFSLock->new($fname,"BLOCKING") or die $!; system $ENV{EDITOR}, $fname ' your-important-file
Re: how can i find whether a file has been opened by some other process across a network(not in a single machine)?
by psychotic (Beadle) on Dec 05, 2005 at 11:06 UTC
    You can either SSH to the box and execute "locally" the above mentioned tool, or you can utilize a client-server architecture, if the potential security risk is acceptable, that performs the needed task. Since i am mostly a Windows user, that's all i can offer.
      Thanks psychotic, My requirement is a part of a script that updates a file thats been used by many applications/users (they canmanually edit them). So, everything has to be done automatically. anyways thanks for your response
        It might be worthwhile mentioning why you need to check if a file is already open, and what exactly you mean by that. For instance, if a program has opened a given file with an advisory flock and expects mutual flocking your script can still read/write it's contents, even if that will perhaps lead to unpredictable results. It all comes down to what you are trying to achieve.

        Sometimes, the solution to a problem comes along easier if the whole picture is presented. It's like a painting. You are showing us a tree, and we say "nice". But you never mention about the beautiful lake right beside it, which makes us go "wow". ;)

Re: how can i find whether a file has been opened by some other process across a network(not in a single machine)?
by rinceWind (Monsignor) on Dec 05, 2005 at 11:11 UTC

    jijogroups,

    Please can you supply more information about your network. I take it you are talking about file sharing over a LAN.

    Are the disks NFS mounted? Is it some kind of cluster configuration? I presume that the machine you are wanting to run the script on has the fs mounted on it.

    --

    Oh Lord, won’t you burn me a Knoppix CD ?
    My friends all rate Windows, I must disagree.
    Your powers of persuasion will set them all free,
    So oh Lord, won’t you burn me a Knoppix CD ?
    (Missquoting Janis Joplin)

      The network is mounted on NFS. most of the machines that we use are diskless.
Re: how can i find whether a file has been opened by some other process across a network(not in a single machine)?
by Moron (Curate) on Dec 05, 2005 at 13:23 UTC
    Module IO::AIO provides interfaces to system calls that can poll all file opens on a particular file.

    -M

    Free your mind

Re: how can i find whether a file has been opened by some other process across a network(not in a single machine)?
by hakkr (Chaplain) on Dec 05, 2005 at 11:50 UTC
    Hi, The linux command 'lsof' will give you a list of all currently open files and their processes.
      lsof doesn't work here
Re: how can i find whether a file has been opened by some other process across a network(not in a single machine)?
by Celada (Monk) on Dec 05, 2005 at 17:55 UTC

    As others have said and as you have discovered, things like fuser and lsof only work on the local machine, which leaves you the option of using voluntary locks. Voluntary locks don't do what you need unless everyone who touches the file makes use of them.

    But, for your purposes, is it enough to know whether or not the file has been accessed recently? If so, you could use the last access time from stat:

    @_ = stat("thefile"); $how_recently = time-$_[8];

    (Some filesystems are mounted without the option for keeping track of the last access time.)