在 Debian 上配置和使用 inotify(Linux 文件系统事件监控机制)通常分为两部分:
下面按常见场景说明。
cat /proc/sys/fs/inotify/max_user_watches
cat /proc/sys/fs/inotify/max_user_instances
cat /proc/sys/fs/inotify/max_queued_events
常见默认值:
max_user_watches:8192(监控文件/目录数)max_user_instances:128max_queued_events:16384sudo sysctl fs.inotify.max_user_watches=524288
sudo sysctl fs.inotify.max_user_instances=512
sudo sysctl fs.inotify.max_queued_events=1048576
编辑配置文件:
sudo nano /etc/sysctl.d/99-inotify.conf
写入内容(示例):
fs.inotify.max_user_watches=524288
fs.inotify.max_user_instances=512
fs.inotify.max_queued_events=1048576
应用配置:
sudo sysctl --system
验证:
cat /proc/sys/fs/inotify/max_user_watches
Debian 默认通常已支持 inotify,但命令行工具需要安装:
sudo apt update
sudo apt install inotify-tools
常用命令:
inotifywait -m /path/to/dir
示例(监控创建、修改、删除):
inotifywait -m -e create -e modify -e delete /var/www
inotifywait -m -r -e close_write /data | while read line; do
rsync -avz /data user@host:/backup
done
inotifywait -m -r -e modify src/ | while read; do
make
done
#include <sys/inotify.h>import inotify.adapters 或 pyinotifychokidar / fs.watchgithub.com/fsnotify/fsnotify→ 提高 max_user_watches
宿主机和容器各自有 inotify 限制,需分别配置
inotify 不支持 NFS 等远程文件系统
如果你有具体用途(如 Docker、同步、日志监控),可以告诉我,我给你更针对性的配置示例。