![如何查看用户打开了多少个 inotify 观察程序?](https://linux22.com/image/183444/%E5%A6%82%E4%BD%95%E6%9F%A5%E7%9C%8B%E7%94%A8%E6%88%B7%E6%89%93%E5%BC%80%E4%BA%86%E5%A4%9A%E5%B0%91%E4%B8%AA%20inotify%20%E8%A7%82%E5%AF%9F%E7%A8%8B%E5%BA%8F%EF%BC%9F.png)
当我在容器中运行命令时,我得到
inotify_init1() failed: Too many open files
如何查看我打开了多少个 inotify 观察程序以及是什么让它们保持打开状态?
答案1
#!/bin/bash -e
cols=$(stty size | awk '{print $2}')
printf "%-13s%-8s%-6s%-5s%s\n" COUNT PPID PID UID CMD
grep ^inotify /proc/*/fdinfo/* 2>/dev/null |
cut -d/ -f3 | sort | uniq -c | sort -n |
while read -r count pid; do
printf "%-10s" "$count"
ps -o ppid=,pid=,uid=,cmd= -p "$pid"
done |
awk '{sum += $1; print} END {print sum}' |
cut -c-"$cols"
echo
sysctl fs.inotify.max_user_watches
示例输出:
COUNT PPID PID UID CMD
1 1303 112217 1000 xfce4-terminal --maximize
1 1 1169 100 /lib/systemd/systemd-timesyncd
1 1238 1255 1000 /usr/bin/dbus-daemon --config-file=/usr/share/defaults/at-spi2/accessibility.conf --nofork --print-address 3
1 1187 1292 1000 xfce4-panel
1 1 1614 0 /usr/lib/clightd/clightd
1 1085 1672 1000 /usr/libexec/gvfs-udisks2-volume-monitor
1 1085 1810 1000 /usr/libexec/gvfs-afc-volume-monitor
1 1085 1903 1000 /usr/libexec/evolution-calendar-factory
1 1085 1958 1000 /usr/libexec/evolution-addressbook-factory
1 1 738 0 /usr/sbin/acpid
1 1 760 0 /lib/systemd/systemd-logind
2 1085 1092 1000 /usr/bin/pulseaudio --daemonize=no --log-target=journal
2 1085 1112 1000 /usr/bin/dbus-daemon --session --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only
2 1085 1278 1000 /usr/bin/gpg-agent --supervised
2 1085 1866 1000 /usr/libexec/evolution-source-registry
3 1 1675 0 /usr/lib/udisks2/udisksd
3 1 742 106 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only
6 1241 1885 1000 /usr/libexec/gvfsd-trash --spawner :1.8 /org/gtk/gvfs/exec_spaw/0
8 1 17770 1000 /usr/lib/firefox/firefox
10 1292 1303 1000 /usr/lib/x86_64-linux-gnu/xfce4/panel/wrapper-2.0 /usr/lib/x86_64-linux-gnu/xfce4/panel/plugins/libwhiskermenu.so 6 12
12 1 332 0 /lib/systemd/systemd-udevd
14 1 1268 0 /usr/lib/policykit-1/polkitd --no-debug
18 1 1085 1000 /lib/systemd/systemd --user
21 1187 1419 1000 xfdesktop
36 1 1291 1000 xfsettingsd
46 0 1 0 /sbin/init
196
fs.inotify.max_user_watches = 2048
答案2
要找出为特定用户打开了多少个 inotify_init 文件,请使用上面的代码片段(替换用户):
find /proc/*/fd -user "<user>" -lname anon_inode:inotify -printf '%hinfo/%f\n' 2>/dev/null | xargs cat | grep -c '^inotify'
您还可以使用inotify-消费者 并将其添加到您的配置文件中的别名中,以便下次使用来找出当前用户 inotify_init 打开的文件数:
inotify-consumers
可以通过设置来增加它们:
echo <new-number> > /proc/sys/fs/inotify/max_user_instances
但这不是永久的;您可以使用 /etc/sysctl.conf 来执行此操作。
echo fs.inotify.max_user_watches=<new-number> | sudo tee -a /etc/sysctl.conf && sudo sysctl -p