温馨提示×

Ubuntu系统中inotify配置步骤

小樊
53
2025-08-17 20:16:14
栏目: 智能运维

Ubuntu系统配置inotify步骤如下:

  1. 安装inotify-tools

    sudo apt update sudo apt install inotify-tools 
  2. 基本监控命令

    • 监控指定目录(如/path/to/directory)的所有事件:
      inotifywait -m /path/to/directory 
    • 递归监控子目录并指定事件类型(如创建、修改、删除):
      inotifywait -m -r -e create,modify,delete /path/to/directory 
  3. 高级配置(可选)

    • 设置超时时间-t 60(单位:秒),超时后自动退出。
    • 输出到文件:通过重定向>将事件记录到日志文件,例如:
      inotifywait -m -r -e create,modify /path/to/directory > events.log 2>&1 & 
  4. 与脚本结合
    编写脚本(如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 
  5. 优化性能(可选)

    • 调整内核参数(如max_user_watches)以支持更大监控规模,编辑/etc/sysctl.conf并添加:
      fs.inotify.max_user_watches=524288 
      然后执行:sudo sysctl -p

以上步骤可快速实现Ubuntu系统下的inotify文件监控功能,满足实时事件捕获与自动化处理需求。

0