linux下通过inotify-tools监控文件系统,用 inotify-tools 监视文件系统事件并对其采取行动...

安装

以 CentOS 为例:

安装 EPEL :

yum install -y epel-release && yum update

安装 inotify-tools:

yum install inotify-tools

在 CentOS-7 中

yum --enablerepo=epel install inotify-tools

v3.14-8.el7.×86_64 as of 4-18-2018

配置

创建 Shell 脚本文件:#!/bin/bash

inotifywait -mrq -e modify,attrib,move,create,delete /要监视的目录 | while read dir event file;

do

curl -d "df=${dir}${file}&ev=${event}" https://xxx.xxx.xxx/api/inotify/

done

并将该文件设置为可执行:chmod +x xxx.sh

注:上述示例将对文件的部分操作事件信息传递到远程接口。inotifywait 与 curl 的用法请自行百度。

如果需要忽略部分文件路径,可以使用正则表达式进行过滤,例:#!/bin/bash

inotifywait -mrq -e modify,attrib,move,create,delete /要监视的目录 | while read dir event file;

do

df=${dir}${file};

if [[ ! $df =~ ^/www/wwwroot/[0-9a-z]+.xxx.com/[0-9a-zA-Z]+/Runtime/Cache/[0-9a-zA-Z]+/[0-9a-f]{32}.php$

&& ! $df =~ ^/www/wwwroot/[0-9a-z]+.xxx.com/[0-9a-zA-Z]+/Runtime/Logs/[0-9a-zA-Z]+/[0-9]{2}_[0-9]{2}_[0-9]{2}.log$

]]; then

curl -d "df=${df}&ev=${event}" https://xxx.xxx.xxx/api/inotify/

else

echo "Ignored: $df"

fi

done

注意:bash 中使用 [[ string =~ regex ]] 表达式进行正则匹配,“!”取反,“&&”为且,书写时不要吝啬使用空格,否则程序可能不会按预期运行。

执行

先直接执行 sh 命令,排查一些错误。Failed to watch /www/wwwroot; upper limit on inotify watches reached!

Please increase the amount of inotify watches allowed per user via `/proc/sys/fs/inotify/max_user_watches'.

因被监视的目录中文件数超过默认值 8192 提示失败,更改该值即可。echo 8192000 > /proc/sys/fs/inotify/max_user_watches

设置开机自动运行,方法请自行百度。

谢谢赞赏 ♥

6e8abd549c150ef67819ab3cdf9e2d0b.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值