在 Debian 上“监控 inotify”通常有两种含义,下面分别说明,并给出常用方法和工具。
如果你是想用 inotify 来监控文件/目录变化,Debian 上常用以下工具:
inotifywait(最常用)来自 inotify-tools 包。
sudo apt install inotify-tools
示例:
inotifywait -m -r -e create,delete,modify /var/log
常用参数:
-m:持续监控-r:递归目录-e:指定事件(create/delete/modify/move 等)systemd 路径单元(适合服务触发)示例:
# /etc/systemd/system/watch.conf
[Path]
PathModified=/tmp/testfile
[Install]
WantedBy=multi-user.target
然后:
systemctl enable --now watch.path
inotify_init(), inotify_add_watch()pyinotify 或 watchdog如果你是想查看 inotify 使用情况、限制、是否存在耗尽问题,可用以下方法:
cat /proc/sys/fs/inotify/max_user_watches
cat /proc/sys/fs/inotify/max_user_instances
cat /proc/sys/fs/inotify/max_queued_events
临时修改:
sudo sysctl fs.inotify.max_user_watches=524288
永久修改:
echo "fs.inotify.max_user_watches=524288" | sudo tee -a /etc/sysctl.conf
sudo sysctl -p
ls -l /proc/*/fd | grep inotify
或:
find /proc/*/fd -type l -exec readlink {} \; | grep inotify
查看某个进程的 inotify 监听:
cat /proc/<pid>/fdinfo/<fd>
sudo perf trace -e inotify*
或使用:
sudo fatrace
max_user_watcheslsof | grep inotify 辅助分析如果你能具体说明:
我可以给你更精确的命令或脚本。