Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

how to know the files opened by a process

by swaroop (Beadle)
on Dec 26, 2005 at 03:38 UTC ( #519048=perlquestion: print w/replies, xml ) Need Help??
swaroop has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

How to know the files opened by a process (similar to unix lsof command ).

  • Comment on how to know the files opened by a process

Replies are listed 'Best First'.
Re: how to know the files opened by a process
by McDarren (Abbot) on Dec 26, 2005 at 03:49 UTC
    This is a pretty open-ended question. To get some useful answers you're probably going to have to be a bit more specific.

    Of course, if you just want access to the functionality of lsof, then you could do something like this:

    #!/usr/bin/perl -w use strict; my $lsof = "/usr/sbin/lsof -u 'root'"; # Change this to suit open DATA, "$lsof|" or die "Could not open $lsof:$!\n"; while (<DATA>) { print; } close DATA;

    Darren :)

      Hi, Thanks.But the problem is , we need to write seperate methods for sun/linux. I'm looking for modules.

        Well, lsof does work on multiple operating systems including Linux and Solaris. For example, lsof revision 4.74 lists information about files opened by processes for the following UNIX dialects:

        AIX 5.[123] Apple Darwin 6.x and 7.x for Power Macintosh systems BSDI BSD/OS 4.3.1 for x86-based systems DEC OSF/1, Digital UNIX, Tru64 UNIX 4.0, and 5.1 FreeBSD 4.[2-9], 4.1[01], 5.[012] and 6.0 for x86-based systems FreeBSD 5.[012] and 6.0 for Alpha, AMD64 and Sparc64 based systems HP-UX 11.00 and 11.11 Linux 2.1.72 and above for x86-based systems NetBSD 1.[456] and 2.x for Alpha, x86, and SPARC-based systems NEXTSTEP 3.[13] for NEXTSTEP architectures OpenBSD 2.[89] and 3.[0123456] for x86-based systems OPENSTEP 4.x Caldera OpenUNIX 8 SCO OpenServer Release 5.0.6 for x86-based systems SCO|Caldera UnixWare 7.1.4 for x86-based systems Solaris 2.6, 8, 9 and 10

        The path to where lsof is installed my vary slightly but you can modify your code to pick the right one depending on which OS you are on at the time. So McDarren's example should give you what you need.

        -- vek --
Re: how to know the files opened by a process
by idsfa (Vicar) on Dec 26, 2005 at 08:49 UTC

    For an OS that has a /proc, you can File::Find on the /proc/\d+/fd/ nodes. They will be a symlink to the opened files of each process currently running.

    # ls -l /proc/####/fd/ total 3 lr-x------ 1 root root 64 Dec 26 02:46 0 -> /proc/kmsg lrwx------ 1 root root 64 Dec 26 02:46 1 -> socket:[11384] lr-x------ 1 root root 64 Dec 26 02:46 2 -> /boot/ +k7

    The intelligent reader will judge for himself. Without examining the facts fully and fairly, there is no way of knowing whether vox populi is really vox dei, or merely vox asinorum. — Cyrus H. Gordon

      This will only give you the opened file descriptors for the process, it does not give you memory-mapped files(/proc/###/maps), the current working directory (/proc/###/cwd) or indeed the binary itself(/proc/###/exe). (All of these are for Linux btw, I have no idea whether this is the same on Sun OS)

      In answer to the OP's question, I once looked for the same thing and don't know of an existing module that does this. If you're looking for a cross-platform solution I'd suggest going with McDarrens suggestion to use the lsof binary and parse its output(that's what I did, I'd post code, but I don't own it and it was bog-standard parsing stuff anyway). It'll be easier to compensate for syntax and output differences of lsof than to mimic its behaviour on different platforms.

      A computer is a state machine. Threads are for people who can't program state machines. -- Alan Cox
Re: how to know the files opened by a process
by jesuashok (Curate) on Dec 26, 2005 at 05:07 UTC

    why can't you search for an application which is similar to

    "Process Explorer"

    for the OS you use.

    "Keep pouring your ideas"

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://519048]
Approved by McDarren
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (8)
As of 2017-02-24 17:50 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (360 votes). Check out past polls.