Linux 如何查明哪些inotify手表已注册?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13758877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I find out what inotify watches have been registered?
提问by frio
I have my inotify watch limit set to 1024 (I think the default is 128?). Despite that, yeoman, Guard and Dropbox constantly fail, and tell me to up my inotify limit. Before doing so, I'd like to know what's consuming all my watches (I have very few files in my Dropbox).
我将 inotify 监视限制设置为 1024(我认为默认值是 128?)。尽管如此,yeoman、Guard 和 Dropbox 不断失败,并告诉我提高我的 inotify 限制。在这样做之前,我想知道是什么消耗了我所有的手表(我的 Dropbox 中的文件很少)。
Is there some area of /proc or /sys, or some tool I can run, to find out what watches are currently registered?
是否有 /proc 或 /sys 的某些区域,或者我可以运行的某些工具来找出当前注册的手表?
回答by zeekvfu
The default maximum number of
inotify
watches is 8192; it can be increased by writing to /proc/sys/fs/inotify/max_user_watches.
You can usesysctl fs.inotify.max_user_watches
to check current value.Use
tail -f
to verify if your OS does exceed theinotify
maximum watch limit.
The internal implementation oftail -f
command uses theinotify
mechanism to monitor file changes.
If you've run out of yourinotify
watches, you'll most likely to get this error:tail: inotify cannot be used, reverting to polling: Too many open files
To find out what
inotify
watches have been registered, you may refer to this, and this. I tried, but didn't get the ideal result. :-(
默认的最大
inotify
监视数为 8192;可以通过写入/proc/sys/fs/inotify/max_user_watches来增加它。
您可以使用sysctl fs.inotify.max_user_watches
来检查当前值。使用
tail -f
验证,如果你的OS不超过inotify
最高限额的手表。命令
的内部实现tail -f
使用了inotify
监视文件更改的机制。
如果你的inotify
手表用完了,你很可能会收到这个错误:tail:inotify 无法使用,恢复为轮询:打开的文件太多
Reference:
https://askubuntu.com/questions/154255/how-can-i-tell-if-i-am-out-of-inotify-watches
https://unix.stackexchange.com/questions/15509/whos-consuming-my-inotify-resources
https://bbs.archlinux.org/viewtopic.php?pid=1340049
参考:
https: //askubuntu.com/questions/154255/how-can-i-tell-if-i-am-out-of-inotify-watches
https://unix.stackexchange.com/questions/15509/whos -消耗我的inotify-resources
https://bbs.archlinux.org/viewtopic.php?pid=1340049
回答by Guido
I think
我认为
sudo ls -l /proc/*/fd/* | grep notify
might be of use. You'll get a list of the pids that have a inotify fd registered.
可能有用。您将获得已注册 inotify fd 的 pid 列表。
I don't know how to get more info than this! HTH
我不知道如何获得比这更多的信息!HTH
回答by David Canós
inotify filesystem options
inotify 文件系统选项
sysctl fs.inotify
sysctl fs.inotify
opened files
打开的文件
lsof | grep inotify | wc -l
lsof | grep inotify | wc -l
Increase the values like this
像这样增加值
sysctl -n -w fs.inotify.max_user_watches=16384
sysctl -n -w fs.inotify.max_user_instances=512
sysctl -n -w fs.inotify.max_user_watches=16384
sysctl -n -w fs.inotify.max_user_instances=512
回答by brenthompson2
The following terminal command worked perfectly for me on my Ubuntu 16.04 Machine:
以下终端命令在我的 Ubuntu 16.04 机器上非常适合我:
for foo in /proc/\*/fd/*; do readlink -f $foo; done |grep inotify |cut -d/ -f3 |xargs -I '{}' -- ps --no-headers -o '%p %U %a' -p '{}' |uniq -c |sort -n
My problem was that I had a good majority of my HDD loaded as a folder in Sublime Text. Between /opt/sublime_text/plugin_host 8992
and /opt/sublime_text/sublime_text
, Sublime had 18 instances of inotify while the rest of my programs were all between 1-3.
我的问题是我的大部分硬盘都作为Sublime Text 中的文件夹加载。在/opt/sublime_text/plugin_host 8992
和之间/opt/sublime_text/sublime_text
,Sublime 有 18 个 inotify 实例,而我的其余程序都在 1-3 之间。
Since I was doing Ionic Mobile App development I reduced the number of instances by 5 by adding the large Node.js folder "node_modules" to the ignore list in the Sublime settings.
由于我在做 Ionic 移动应用程序开发,我通过将大型 Node.js 文件夹“node_modules”添加到 Sublime 设置中的忽略列表,将实例数量减少了 5。
"folder_exclude_patterns": [".svn", ".git", ".hg", "CVS", "node_modules"]
Source: https://github.com/SublimeTextIssues/Core/issues/1195
回答by cincodenada
Since this is high in Google results, I'm copy-pasting part of my answerfrom a similar question over on the Unix/Linux StackExchange:
由于这在 Google 搜索结果中很高,我复制粘贴了 Unix/Linux StackExchange 上类似问题的部分答案:
I ran into this problem, and none of these answers give you the answer of "how many watchesis each process currently using?" The one-liners all give you how many instancesare open, which is only part of the story, and the trace stuff is only useful to see new watches being opened.
我遇到了这个问题,这些答案都没有给你“每个进程当前使用多少个手表?”的答案。one-liners 都给你打开了多少个实例,这只是故事的一部分,trace 的东西只对看到新手表打开有用。
This will get you a file with a list of open inotify
instances and the number of watchesthey have, along with the pids and binaries that spawned them, sorted in descending order by watch count:
这将为您提供一个文件,其中包含打开的inotify
实例列表和它们拥有的监视数量,以及生成它们的 pid 和二进制文件,按监视计数降序排序:
sudo lsof | awk '/anon_inode/ { gsub(/[urw]$/,"",); print "/proc/""/fdinfo/"; }' | while read fdi; do count=$(sudo grep -c inotify $fdi); exe=$(sudo readlink $(dirname $(dirname $fdi))/exe); echo -e $count"\t"$fdi"\t"$exe; done | sort -nr > watches
If you're interested in what that big ball of mess does and why, I explained in depth over on the original answer.
如果您对那团乱七八糟的大球做了什么以及为什么感兴趣,我会深入解释原始答案。
回答by oligofren
I already answered thisin the same thread on Unix Stackexchange as was mentioned by @cinkodenada, but thought I could repost my ready-made answer here, seeing that no one really has something that works:
我已经在@cinkodenada 提到的 Unix Stackexchange 上的同一个线程中回答了这个问题,但我想我可以在这里重新发布我的现成答案,因为没有人真正有一些有用的东西:
I created a premade script, inotify-consumers
, which is pretty fast (< 0.1 sec), that lists the top offenders for you:
我创建了一个预制脚本, inotify-consumers
,它非常快(< 0.1 秒),它为您列出了最严重的罪犯:
$ inotify-consumers
INOTIFY
WATCHER
COUNT PID CMD
----------------------------------------
6688 27262 /home/dvlpr/apps/WebStorm-2018.3.4/WebStorm-183.5429.34/bin/fsnotifier64
411 27581 node /home/dvlpr/dev/kiwi-frontend/node_modules/.bin/webpack --config config/webpack.dev.js
79 1541 /usr/lib/gnome-settings-daemon/gsd-xsettings
30 1664 /usr/lib/gvfs/gvfsd-trash --spawner :1.22 /org/gtk/gvfs/exec_spaw/0
14 1630 /usr/bin/gnome-software --gapplication-service
Here you quickly see why the default limit of 8K watchers is too little on a development machine, as just WebStorm instance quickly maxes this when encountering a node_modules
folder with thousands of folders. Add a webpack watcher to guarantee problems ...
在这里,您很快就会明白为什么在开发机器上 8K 观察者的默认限制太少了,因为当遇到一个node_modules
包含数千个文件夹的文件夹时,WebStorm 实例会迅速达到最大值。添加一个 webpack watcher 来保证问题......
Just copy the contents of the script (or the file on GitHub) and put it somewhere in your $PATH
, like /usr/local/bin
. For reference, the main content of the script is simply this
只需复制脚本(或在GitHub上的文件)的内容并把它放在你的$PATH
一样/usr/local/bin
。供参考,脚本的主要内容就是这个
find /proc/*/fd \
-lname anon_inode:inotify \
-printf '%hinfo/%f\n' 2>/dev/null \
\
| xargs grep -c '^inotify' \
| sort -n -t: -k2 -r
In case you are wondering how to increase the limits, here's how to make it permanent:
如果您想知道如何增加限制,以下是使其永久化的方法:
echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf
sudo sysctl -p
回答by Nicolas
Based on the excellent analysis of cincodenada, I made my own one-liner, which works better for me:
基于cincodenada的优秀分析,我制作了自己的 one-liner,对我来说效果更好:
find /proc/*/fd/ -type l -lname "anon_inode:inotify" -printf "%hinfo/%f\n" | xargs grep -cE "^inotify" | column -t -s:
It helps to find all inotify watchers and their watching count. It does not translate process ids to their process names or sort them in any way but that was not the point for me. I simply wanted to find out which process consumes most of the watches. I then was able to search for that process using its process id.
它有助于找到所有 inotify 观察者及其观看次数。它不会将进程 ID 转换为它们的进程名称或以任何方式对它们进行排序,但这对我来说不是重点。我只是想找出哪个过程消耗了大部分手表。然后我就可以使用它的进程 id 搜索该进程。
You can omit the last column
command if you don't have it installed. It's only there to make the output look nicer.
column
如果你没有安装它,你可以省略最后一个命令。它只是为了使输出看起来更好。
Okay, as you can see, there is a similar and less fork hungry approach from @oligofren. Better you use his simple script. It's very nice. I was also able to shrink my one-liner because I was not aware of the -lname
parameter of find
which comes in very handy here.
好的,正如您所看到的,@oligofren 提供了一种类似且不那么渴望分叉的方法。最好使用他的简单脚本。这是很不错的。我还能够缩小我的单线,因为我不知道哪个-lname
参数find
在这里非常方便。