Ubuntu系统配置inotify步骤如下:
安装inotify-tools
sudo apt update sudo apt install inotify-tools 基本监控命令
/path/to/directory)的所有事件:inotifywait -m /path/to/directory inotifywait -m -r -e create,modify,delete /path/to/directory 高级配置(可选)
-t 60(单位:秒),超时后自动退出。>将事件记录到日志文件,例如:inotifywait -m -r -e create,modify /path/to/directory > events.log 2>&1 & 与脚本结合
编写脚本(如monitor.sh)处理监控事件,示例内容:
#!/bin/bash DIRECTORY="/path/to/directory" inotifywait -m -r -e create,modify,delete --format '%w%f %e' "$DIRECTORY" | while read FILE EVENT; do echo "文件 $FILE 发生事件:$EVENT" # 可在此处添加自定义操作(如备份、通知等) done 赋予执行权限并运行:
chmod +x monitor.sh ./monitor.sh 优化性能(可选)
max_user_watches)以支持更大监控规模,编辑/etc/sysctl.conf并添加:fs.inotify.max_user_watches=524288 然后执行:sudo sysctl -p。以上步骤可快速实现Ubuntu系统下的inotify文件监控功能,满足实时事件捕获与自动化处理需求。