Подсчет количества открытых файлов для каждого пользователя?

1155
sorin

Я ищу команду, которая перечисляла бы мне количество открытых файлов для каждого пользователя, отсортированных по убыванию.

Например lsof -u postgres | wc -, перечислил бы их только для пользователя postgres, но я хочу увидеть, на каком пользователе я могу потерять обработчики.

Примечание: некоторые вопросы могут относиться к PID, а не к именам пользователей, но, тем не менее, я ищу один вкладыш.

1

2 ответа на вопрос

1
Warren Lavallee

If you are open to pipe's, how about this:

lsof | perl -pe 's/\s\s+/ /g' | cut -d' ' -f3 | grep -v ^USER | sort | uniq -c | sort -rn

I tested this on MacOS 10.10.2 (Yosemite), OEL 6.6, and Ubuntu 10.04.01.

1
AFH

I ran Warren Lavallee's script, and found it difficult to interpret.

Instead, I extended the original script to step through all the users to give the following:-

for f in $(sed</etc/passwd 's/:.*$//g'); do ( echo -n $f ' '; lsof -u $f 2>/dev/null | wc -l ); done | grep -v ' 0$' 

This works fine on Ubuntu 14.04. The final grep removes the zero entries, as a lot of the users will not have active processes. You can also add | sort -rnk 2 to the end of the command to sort into descending file count order.

You will need to be in a root shell to make sure you can see the files from all users.

Похожие вопросы