1

I knows I can use the command

lsof -p xxxx | wc -l

to know the count of opened files op a process, it works, but however, it is just too inefficient. I have some server process which have too many socket files, the wc -l method never return the result. So, what is the efficient way to know how many files opened on a process?

Thanks.

3 Answers 3

1

There is a proc interface for open files: /proc/PID/fd/. It is a directory of symlinks. Any open sockets will appear to be linked to a file named "socket:[INODE NUMBER]"

0

Use ps -C <progname> -o pid= | wc -l where progname is the name of the process in a ps-listing.

2
  • 1
    That gives a count of processes not the count of open files. Jun 10, 2010 at 19:07
  • Sorry, my explanation wasn't specific enough. As described by Andrew E. Falcon, these are file descriptor PIDs. You can see which file is behind a PID by running it that way: for pid in $(ps -C <progname> -o pid=); do ls -l /proc/$pid/fd; done
    – weeheavy
    Jun 14, 2010 at 8:34
0

Some context (use-case scenarios) may provide better direction for answers.

Documentation of lsof describes a few methods that it calls that may block for undesirable amounts of time. I would suggest glancing over the BLOCKS AND TIMEOUTS section of the lsof man page.

You might try the following and see if it provides desirable results:

lsof -bwp $pid | wc -l

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .