系統當前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)的,所以不能將線程也統計到開放文件數中。