提升 Ubuntu 上 inotify 稳定性的实用方案
一 内核参数与系统资源
cat /proc/sys/fs/inotify/{max_user_watches,max_user_instances,max_queued_events}sudo sysctl -w fs.inotify.max_user_watches=524288sudo sysctl -w fs.inotify.max_user_instances=256sudo sysctl -w fs.inotify.max_queued_events=32768echo "fs.inotify.max_user_watches=524288" | sudo tee -a /etc/sysctl.confecho "fs.inotify.max_user_instances=256" | sudo tee -a /etc/sysctl.confecho "fs.inotify.max_queued_events=32768" | sudo tee -a /etc/sysctl.confsudo sysctl -p二 监控范围与事件过滤
inotifywait -m -r --exclude '/tmp/' --exclude '\.tmp$' /path(忽略临时目录与 .tmp 文件)。三 应用侧处理与架构
四 监控 排查与运维
cat /proc/sys/fs/inotify/max_user_watches 等;若接近上限,优先做范围与事件过滤,再考虑上调参数。dstat、vmstat、iostat 等查看 CPU、I/O、内存与负载;必要时用 strace 跟踪 inotify 相关系统调用,定位慢操作与异常路径。max_user_watches;若仍不足,评估是否拆分监控或改用更高层工具。五 常见场景配置示例
watchOptions: { ignored: /node_modules/ }node_modules、dist、logs、tmp 等目录。inotifywait -m -r --exclude '/tmp/' --exclude '\.tmp$' --event MODIFY,CREATE /path/to/monitor