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

Just a curiosity question. Is there a way in perl to list out who has a file open? As in somebody complains a file is locked and you want to see by who....

Replies are listed 'Best First'.
Re: List who has a file open?
by maverick (Curate) on Jan 25, 2002 at 04:35 UTC
    If you're on a Un*x try 'lsof'. From the man page it says it's on:
    AIX 4.1.[45], 4.2[.1], and 4.3[.123] BSDI BSD/OS 2.1, 3.[01], and 4.[01] for Intel-based systems DC/OSx 1.1 for Pyramid systems DEC OSF/1, Digital UNIX, Tru64 UNIX 2.0, 3.2, 4.0, and 5.[01] FreeBSD 2.1.[67], 2.2[.x], 3.[012345], 4.[01], and 5.0 for Intel-based systems HP-UX 9.01, 10.20, and 11.00 Linux 2.0.3[2346] and 2.[1234].x for Intel-based systems NetBSD 1.[2345] for Alpha, Intel, and SPARC-based systems NEXTSTEP 3.[13] for NEXTSTEP architectures OpenBSD 2.[01234567] for Intel-based systems OpenStep 4.x Reliant UNIX 5.4[34] for Pyramid systems SCO OpenServer Release 3.0 and 5.0.[02456] for Intel-based systems SCO UnixWare 2.1.[123] and 7[[.0].1] for Intel-based systems Sequent PTX 2.1.9, 4.2.[13], 4.[34], 4.4[.1246], and 4.5[.1] for Seque +nt systems Solaris 2.5.1, 2.6, 7, 8 BETA, and 8 BETA-Refresh SunOS 4.1.x Ultrix 4.2
    You could call it via:
    open(LSOF,"/usr/sbin/lsof MY_OPTIONS_HERE |") || die $!; while(<LSOF>) { # so stuff }
    or
    @output = `/usr/sbin/lsof MY_OPTIONS_HERE`;
    HTH

    /\/\averick
    perl -l -e "eval pack('h*','072796e6470272f2c5f2c5166756279636b672');"

Re: List who has a file open?
by belg4mit (Prior) on Jan 25, 2002 at 04:32 UTC
    Look at http://freshmeat.net for lsof. I imagine it simply iterates through /proc to find the filehandles opened by each process.

    --
    perl -pe "s/\b;([st])/'\1/mg"

Re: List who has a file open?
by Kanji (Parson) on Jan 25, 2002 at 04:37 UTC

    You might be able to pull such info out of /proc or whatever your OS-equivalent might be, but it's undoubtably easier to system or exec an external binary like fstat (BSD), fuser (Solaris), lsof (generic Unicies) ... I'm sure similar tools exist for other OSes.

        --k.


      redhat , mandrake, and debian linux distros as well as solairs and probbably other Unices include fuser, which is my tool of choice for obtaining this info, from the man page : 'fuser displays the PIDs of processes using the specified files or file systems. In the default display mode, each file name is followed by a letter denoting the type of access' -Tom
        thanks! Found the Pstools set that will help out with the PC side of things!
Re: List who has a file open?
by japhy (Canon) on Jan 25, 2002 at 04:21 UTC
    Sorry, but no operating system I know of stores that kind of information. Sorry, but I didn't know of the lsof command. That was a bit ignorant of me.

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;