在Linux中为特定用户运行的每个进程有多少个打开的文件

tes*_*les 12 linux lsof

在Linux上运行Apache和Jboss,有时我的服务器意外停止说问题是太多打开文件.

I know that we might set a higher limit for nproc and nofile at /etc/security/limits.conf to fix the open files problem, but I am trying to get better output, such as using watch to monitor them in real-time.

With this command line I can see how many open files per PID:

lsof -u apache | awk '{print $2}' | sort | uniq -c | sort -n
Run Code Online (Sandbox Code Playgroud)

Output (Column 1 is # of open files for the user apache):

1     PID
1335  13880
1389  13897
1392  13882
Run Code Online (Sandbox Code Playgroud)

If I could just add the watch command it would be enough, but the code below isn't working:

watch lsof -u apache | awk '{print $2}' | sort | uniq -c | sort -n
Run Code Online (Sandbox Code Playgroud)

rea*_*000 5

你应该把命令内部引用如下:

watch 'lsof -u apache | awk '\''{print $2}'\'' | sort | uniq -c | sort -n'
Run Code Online (Sandbox Code Playgroud)

或者您可以将命令放入像test.sh这样的shell脚本中,然后使用watch.

chmod +x test.sh
watch ./test.sh
Run Code Online (Sandbox Code Playgroud)


Gea*_*phy 4

此命令将告诉您 Apache 打开了多少个文件:

ps -A x |grep apache | awk '{print $1}' | xargs -I '{}' ls /proc/{}/fd  | wc -l
Run Code Online (Sandbox Code Playgroud)

您可能必须以 root 身份运行它才能访问进程 fd 目录。这听起来好像您有一个没有关闭其文件描述符的 Web 应用程序。我会把精力集中在那个领域。