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

Hi, Does anyone know if there is a perl module which tells you if a file is currently open (similiar to the lsof tool) ? Thanks for help
  • Comment on looking for perl module which returns if a file is open

Replies are listed 'Best First'.
Re: looking for perl module which returns if a file is open
by valdez (Monsignor) on Jul 01, 2003 at 15:19 UTC

    Do you mean something like the command fuser? If so, there is a module on CPAN called Linux::Fuser:

    This module provides information similar to the Unix command 'fuser' about which processes have a particular file open. The way that this works is highly unlikely to work on any other OS other than Linux and even then it may not work on other than 2.2.* kernels.

    It should also be borne in mind that this may not produce entirely accurate results unless you are running the program as the Superuser as the module will require access to files in /proc that may only be readable by their owner.

    Added: If you need instead to parse the output of lsof, use the -F flag that will force the program to produce its output in a format parseable by other programs.

    HTH, Valerio

      As author of Linux::Fuser I would be interested to know whether anyone has had any success with it on an OS version other than that stated in the README. Let me know at the address in the README if you have. Cheers.

      /J\
      
Re: looking for perl module which returns if a file is open
by Joost (Canon) on Jul 01, 2003 at 15:14 UTC
    AFAIK there's no module that does this, so I'd just go for parsing the output of lsof; something like:
    my @opened_by_X = grep /^X\s+/,split /\n/,`/usr/sbin/lsof`;
    Ofcourse nobody's stopping you from turning this into a module yourself. :-)

    Joost.

    -- #!/usr/bin/perl -np BEGIN{@ARGV=$0}s(^([^=].*)|=)()s; =Just another perl hacker\