inotifywait监听php,用inotifywait监视文件变化并执行相应脚本

Inotify 是一个 Linux特性,它监控文件系统操作,比如读取、写入和创建。Inotify 反应灵敏,用法非常简单,并且比 cron 任务的繁忙轮询高效得多。在内核 2.6.13 以上都可以使用。

Inotify一种强大的、细粒度的、异步文件系统监控机制,它满足各种各样的文件监控需要,可以监控文件系统的访问属性、读写属性、权限属性、删除创建、移动等操作,也就是可以监控文件发生的一切变化。

inotify-tools是一个C库和一组命令行的工作提供Linux下inotify的简单接口。inotify-tools安装后会得到inotifywait和inotifywatch这两条命令:

一是 inotifywait,它是用来监控文件或目录的变化,二是inotifywatch,它是用来统计文件系统访问的次数。

我家里安装了一个网络摄像机,用来监控家里的的入口是否有异常,如果有异常就会自动录像。录像文件存放在nas服务器上,为了把文件做备份,我又把录像文件定时复制到远程云服务器上。之前是采用定时备份的模式,实时性不好。为了保证备份的实时性,我查找了linux对文件的监控功能的资料,终于发现了inotify-tools这个工具。通过对inotify-tools的资料的学习,编写了一个自动执行脚本,当检测到摄像头保存视频的文件夹有变动,就自动用rsync把更改的文件同步到远程云服务器上。

下面就是安装和使用步骤:

1. 安装inotify-tools

家里的服务安装的是debian 10系统,是可以直接安装inotify-tools工具的

#apt update

#apt install inotify-tools

就是这么简单,看网上很多文章都写的是从github下载.gz文件,然后解压缩,再&……%&*&……,其实完全不用这么麻烦,就只需要上面两行命令就完成了。

然后执行inotifywatch --help,有输出命令说明就表明工具安装正常,可以使用了。

#inotifywatch --help

inotifywatch 3.14

Gather filesystem usage statistics using inotify.

Usage: inotifywatch [ options ] file1 [ file2 ] [ ... ]

Options:

-h|--help Show this help text.

-v|--verbose Be verbose.

@ Exclude the specified file from being watched.

--fromfile

Read files to watch from or `-' for stdin.

--exclude

Exclude all events on files matching the extended regular

expression .

--excludei

Like --exclude but case insensitive.

-z|--zero

In the final table of results, output rows and columns even

if they consist only of zeros (the default is to not output

these rows and columns).

-r|--recursive Watch directories recursively.

-t|--timeout

Listen only for specified amount of time in seconds; if

omitted or 0, inotifywatch will execute until receiving an

interrupt signal.

-e|--event [ -e|--event ... ]

Listen for specific event(s). If omitted, all events are

listened for.

-a|--ascending

Sort ascending by a particular event, or `total'.

-d|--descending

Sort descending by a particular event, or `total'.

Exit status:

0 - Exited normally.

1 - Some error occurred.

Events:

access file or directory contents were read

modify file or directory contents were written

attrib file or directory attributes changed

close_write file or directory closed, after being opened in

writable mode

close_nowrite file or directory closed, after being opened in

read-only mode

close file or directory closed, regardless of read/write mode

open file or directory opened

moved_to file or directory moved to watched directory

moved_from file or directory moved from watched directory

move file or directory moved to or from watched directory

create file or directory created within watched directory

delete file or directory deleted within watched directory

delete_self file or directory was deleted

unmount file system containing file or directory unmounted

2. inotifywait使用

这里直接给出我的脚本代码,再做一个说明吧!

#!/bin/bash

dir=/ipcam #指定需要监视的文件夹

log_file=/watch.log #指定输出信息的文件,方便后面查看

rsync_file=/xxx.sh #发现文件有变化时需要执行的脚本文件

while

inotifywait -r $dir -o $log_file -e close \ #这里只监视文件的close动作

--timefmt '%d/%m/%y %H:%M' --format '%T %w %f %e';

do

bash $rsync_file #执行同步文件的脚本

sleep 10m #等待10分钟,如果不加这个命令,同步会非常频繁,没必要

done

刚开始的时候因为不清楚inotifywait的执行机制,代码写的太复杂,反而出现问题,后来反复测试发现可以简化,就去掉了很多不必要的命令,这样流程也很清楚。

我的rsync脚本文件就是用rsync命令把视频文件同步到云服务器上,大家自行查找rsync的相关资料,这里就不再累述。

3. inotifywatch的使用

inofitywatch是用来监控文件或文件夹的变化,并输出统计信息的。

比如:

# inotifywatch /ipcam -t 300

Establishing watches...

Finished establishing watches, now collecting statistics.

total access close_nowrite open filename

51 25 13 13 /ipcam/

这里监控了5分钟时间,总共出现了51次文件变更,其中access 25次,close_nowrite 13次,open 13次,结果清晰明了。

4. 最后,也是最重要的一点,设置开机启动脚本

#crontab -e

然后添加下面一行

@reboot /xxx/xxx.sh

Perfact!

当然inotifywait和inotifywatch还有很多参数可以设置,功能也很强大,这里我只用了冰山一角就完成了我的任务。很好,很强大!!

置于inotify-tools的资料,可以自己找“男人”问问!

468b9eee006d

image.png

#man inotifywait

这个是最权威的资料,前提是你喜欢english,当然现在网络上也有成堆的中文解释和说明可以阅读,就看个人习惯吧!

最后,庆祝下我这篇简书处女作的诞生!!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值