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.

link|improve this question

20% accept rate
feedback

3 Answers

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]"

link|improve this answer
feedback

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

link|improve this answer
1  
That gives a count of processes not the count of open files. – Dennis Williamson Jun 10 '10 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 '10 at 8:34
feedback

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
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.