系统当前open file总数
lsof -Ki|wc -l
查看某个pid的open files数量
#以下命令需要减1,因为多了个列头
lsof -p [pid]|wc -l
lsof -Ki|grep [pid]|wc -l
也可以通过系统快照进行查看(第一列是总数,第二列是申请了没使用的数量,第三列是系统当前上限):
cat /proc/sys/fs/file-nr
根据open file数量倒序查看前六名的pid(第一列是数量,第二列是pid,head -6是查看前六名)
lsof -Ki|awk '{print $2}'|sort|uniq -c|sort -n -r|head -6
参考内容:
https://www.kernel.org/doc/Documentation/sysctl/fs.txt
https://unix.stackexchange.com/questions/176967/why-file-nr-and-lsof-count-on-open-files-differs
https://stackoverflow.com/questions/6223776/threads-and-file-descriptors
man lsof
-K k selects the listing of tasks (threads) of processes, on dialects where task (thread) reporting is supported. (If help output - i.e., the output of the -h or -? options - shows this option, then task (thread) reporting is
supported by the dialect.)
If -K is followed by a value, k, it must be ``i''. That causes lsof to ignore tasks, particularly in the default, list-everything case when no other options are specified.
说明:
网上大多数直接统计lsof命令执行结果,不加任何参数的都是在误人子弟
lsof不加参数 会列出进程的线程,即TID(threads of processes),而线程和进程是共享文件描述符( file descriptors)的,所以不能将线程也统计到开放文件数中。