2022 年 10 月 31 日更新
While my script below works fine as it is, Michael Sartain implemented a native executable https://github.com/mikesart/inotify-info that is much faster, along with additional functionality not present in my script (below). Worth checking out if you can spend a few seconds compiling it! I have also added contributed some PRs to align the functionality, so it should be pretty 1:1, just faster.
Upvote his answer on the Unix Stackexchange https://unix.stackexchange.com/a/669042/18594.
带脚本的原始答案
I 已经回答了这个 https://unix.stackexchange.com/a/502812/18594在@cincodenada提到的Unix Stackexchange上的同一线程中,但我想我可以在这里重新发布我现成的答案,因为没有人真正拥有有效的东西:
我有一个预制脚本 https://github.com/fatso83/dotfiles/blob/master/utils/scripts/inotify-consumers, inotify-consumers
,其中列出了最严重的违规者:
INOTIFY INSTANCES
WATCHES PER
COUNT PROCESS PID USER COMMAND
------------------------------------------------------------
21270 1 11076 my-user /snap/intellij-idea-ultimate/357/bin/fsnotifier
201 6 1 root /sbin/init splash
115 5 1510 my-user /lib/systemd/systemd --user
85 1 3600 my-user /usr/libexec/xdg-desktop-portal-gtk
77 1 2580 my-user /usr/libexec/gsd-xsettings
35 1 2475 my-user /usr/libexec/gvfsd-trash --spawner :1.5 /org/gtk/gvfs/exec_spaw/0
32 1 570 root /lib/systemd/systemd-udevd
26 1 2665 my-user /snap/snap-store/558/usr/bin/snap-store --gapplication-service
18 2 1176 root /usr/libexec/polkitd --no-debug
14 1 1858 my-user /usr/bin/gnome-shell
13 1 3641 root /usr/libexec/fwupd/fwupd
...
21983 WATCHES TOTAL COUNT
INotify instances per user (e.g. limits specified by fs.inotify.max_user_instances):
INSTANCES USER
----------- ------------------
41 my-user
23 root
1 whoopsie
1 systemd-ti+
...
在这里,您很快就会明白为什么 8K 观察者的默认限制在开发机器上太小,因为只有 WebStorm 实例在遇到node_modules
包含数千个文件夹的文件夹。添加 webpack 观察器以保证出现问题...
尽管我最初制作时它比其他替代方案快得多,但 Simon Matter 为重负载的 Big Iron Linux(数百个核心)添加了一些速度增强功能,极大地加快了速度,从 10 分钟(!)缩短到 15 分钟在他的怪物装备上几秒钟。
稍后的,Brian Dowling 贡献了每个进程的实例数 https://github.com/fatso83/dotfiles/pull/10,以相对较高的运行时间为代价。这对于运行时间约为一秒的普通机器来说是微不足道的,但如果你有 Big Iron,你可能需要早期版本 https://github.com/fatso83/dotfiles/blob/43777e52c49f48ccb6b513b4d4772c4e3cbd6211/utils/scripts/inotify-consumers大约是系统时间的 1/10 :)
如何使用
inotify-consumers --help
???? 要将其安装到您的计算机上,只需复制脚本的内容并将其放在您的计算机中的某个位置即可$PATH
, like /usr/local/bin
。或者,如果您信任网络上的这个陌生人,您可以避免复制它并通过 http 将其传输到 bash:
$ curl -s https://raw.githubusercontent.com/fatso83/dotfiles/master/utils/scripts/inotify-consumers | bash
INOTIFY
WATCHER
COUNT PID USER COMMAND
--------------------------------------
3044 3933 myuser node /usr/local/bin/tsserver
2965 3941 myuser /usr/local/bin/node /home/myuser/.config/coc/extensions/node_modules/coc-tsserver/bin/tsserverForkStart /hom...
6990 WATCHES TOTAL COUNT
它是如何工作的?
作为参考,脚本的主要内容就是这样(灵感来自这个答案 https://unix.stackexchange.com/a/501952/18594)
find /proc/*/fd \
-lname anon_inode:inotify \
-printf '%hinfo/%f\n' 2>/dev/null \
\
| xargs grep -c '^inotify' \
| sort -n -t: -k2 -r
改变限制
如果您想知道如何增加限制
$ inotify-consumers --limits
Current limits
-------------
fs.inotify.max_user_instances = 128
fs.inotify.max_user_watches = 524288
Changing settings permanently
-----------------------------
echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf
sudo sysctl -p # re-read config