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

Hi,

I need to write a script which processes some files. Now the input files are created by another script in append mode. What I need is info as to how can I check whether the input files are still open or not.

Example:

my input files are in directory

/opt/test/data/

with extension as ".dat"

"script1.pl" is creating these files in append mode. i.e. if data is too large then FILE_HANDLE for those files are open in "script1.pl"

"script2.pl" needs these files as input but should not read it if read by another process.

My query is:- How can I check whether script1.pl is still creating those files? Is there a perl internal command or some modules to do that?

Thanks
AvantA
  • Comment on check which files are open to write/read by another process

Replies are listed 'Best First'.
Re: check which files are open to write/read by another process
by cdarke (Prior) on Nov 24, 2010 at 08:41 UTC
    This issue is operating system specific. The path syntax used in your question indicates you are running on some sort of UNIX.

    On Linux it is fairly easy if you know the PID that the script is running under. You can interrogate /proc/PID/fd, then follow the links using readlink. Alternatively you can use Linux::Inotify2 to monitor the directory being written to.

    On non-Linux UNIX systems iNotify does not exist, and so far as I know many systems do not provide the links in /proc/PID/fd. In those cases you might have to resort to running strace(1) or truss(1) programs (only tracing the open(2) calls) in a pipe then interrogating the output. I have used this method, but it is messy.
      Actually, most non-Linux Unix OSses (and even most Linux distros) have at least one of fuser or lsof (the latter stands for 'list open files'...). Of course, to get that information about processes run by a different UID than you currently are, you need root privs.
        fuser is in the POSIX standard, lsof is not.
Re: check which files are open to write/read by another process
by kcott (Archbishop) on Nov 24, 2010 at 03:25 UTC

    flock may do what you want.

    -- Ken

      No, it won't. It will only tell you if a specific file is locked. And only if you have permission to open said file.

      Thanks Ken, but here I forgot to mention another issue. If i apply "flock" I need to apply a shared lock in "script1.pl" and in my case its unfavourable to make any changes in that script. I can change only "script2.pl"
      Thanks
      AvantA
Re: check which files are open to write/read by another process
by samarzone (Pilgrim) on Nov 24, 2010 at 07:40 UTC

    If both the scripts are run by a common user or if second script is called by root you can see the output of linux command lsof to check the files currently open.

Re: check which files are open to write/read by another process
by locked_user sundialsvc4 (Abbot) on Nov 24, 2010 at 14:17 UTC

    Since you can get into a whole mess of conditions that are difficult to anticipate (e.g. security, filesystem types, the phase of the moon ...), not to mention race conditions, I have found that the simplest way is simply to attempt to open the file, requesting an exclusive access-mode if you can.

    Wrap the whole thing in an eval{} block, then test the error-variable ($@) to see if it has a non-empty value.   Also check that the return-code and so forth, but do both.

Re: check which files are open to write/read by another process
by Anonymous Monk on Nov 24, 2010 at 05:20 UTC
    Hi,

    If you are on *nix, you could make a system call to the fuser command.

    J.C.