inotifywait监听php,inotifywait命令

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

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

inotifywait命令可以用来收集有关文件访问信息,Linux发行版一般没有包括这个命令,需要安装inotify-tools,这个命令还需要将inotify支持编译入Linux内核,好在大多数Linux发行版都在内核中启用了inotify。

inotifywatch命令用于收集关于被监视的文件系统的统计数据,包括每个 inotify 事件发生多少次。

开始之前需要检测系统内核是否支持inotify:

使用uname -r命令检查Linux内核,如果低于2.6.13,就需要重新编译内核加入inotify的支持。

使用ll /proc/sys/fs/inotify命令,是否有以下三条信息输出,如果没有表示不支持。

ll /proc/sys/fs/inotify

total 0

-rw-r--r-- 1 root root 0 Jan  4 15:41 max_queued_events

-rw-r--r-- 1 root root 0 Jan  4 15:41 max_user_instances

-rw-r--r-- 1 root root 0 Jan  4 15:41 max_user_watches

安装inotify-tools

inotify-tools项目地址:https://github.com/rvoicilas/inotify-tools

inotify-tools下载地址:https://github.com/rvoicilas/inotify-tools/archive/3.20.1.tar.gz

#CentOS release 64位:

tar zxvf inotify-tools-3.20.1.tar.gz

cd inotify-tools-3.20.1

./configure

make

make install

其它Linux发行版安装如下:

Mageia

inotify-tools is available in the Mageia core repository. You can install it with:

urpmi inotify-tools

Fedora

inotify-tools is available through the Fedora Extras repository. Just do:

yum install inotify-tools

Adel Gadllah is the Fedora package maintainer.

Gentoo

inotify-tools is available in Gentoo’s official portage tree. You can simply run:

emerge inotify-tools

to install it

Lars Wendler is the Gentoo package maintainer.

Debian/Ubuntu

inotify-tools is available in Debian’s official repositories. You can install it by:

apt-get install inotify-tools

Ryan Niebur is the Debian package maintainer.

Void Linux

inotify-tools is available in Void Linux. It can be installed with the following command:

xbps-install inotify-tools

Enno Boland is the Void Linux package maintainer.

FreeBSD

inotify-tools is available as a port and package. It can be installed with the following command:

pkg install inotify-tools

or

cd /usr/ports/sysutils/inotify-tools/ && make install clean

Yuri is the FreeBSD package maintainer.

ALT Linux

inotify-tools is available in the Sisyphus repository. With ALT Linux Sisyphus you can install by the following command:

apt-get install inotify-tools

Igor Zubkov is the ALT Linux Sisyphus package maintainer.

Frugalware Linux

inotify-tools is available in Frugalware Linux. It can be installed with the following command:

pacman-g2 -S inotify-tools

Priyank Gosalia is the Frugalware Linux package maintainer.

Arch Linux

inotify-tools is available in Arch Linux. It can be installed with the following command:

pacman -S inotify-tools

Sergej Pupykin is the Arch Linux package maintainer.

Source Mage GNU/Linux

inotify-tools can be installed with the following command:

cast inotify-tools

Package maintained by the Source Mage team.

ROSA Linux

inotify-tools can be installed from the Contrib repository by running:

urpmi inotify-tools

Denis Silakov is the ROSA Linux package maintainer.

Slackware

comes with inotify-tools by default

PLD Linux

inotify-tools is available in PLD Linux. It can be installed with the following command:

poldek -u inotify-tools

inotify相关参数

inotify定义了下列的接口参数,可以用来限制inotify消耗kernel memory的大小。由于这些参数都是内存参数,因此,可以根据应用需求,实时的调节其大小:

/proc/sys/fs/inotify/max_queued_evnets表示调用inotify_init时分配给inotify instance中可排队的event的数目的最大值,超出这个值的事件被丢弃,但会触发IN_Q_OVERFLOW事件。

/proc/sys/fs/inotify/max_user_instances表示每一个real user id可创建的inotify instatnces的数量上限。

/proc/sys/fs/inotify/max_user_watches表示每个inotify instatnces可监控的最大目录数量。如果监控的文件数目巨大,需要根据情况,适当增加此值的大小。

根据以上在32位或者64位系统都可以执行:

echo 104857600 > /proc/sys/fs/inotify/max_user_watches

echo 'echo 104857600 > /proc/sys/fs/inotify/max_user_watches' >> /etc/rc.local

如果遇到以下错误:

inotifywait: error while loading shared libraries: libinotifytools.so.0: cannot open shared object file: No such file or directory

解决方法:

32位系统:ln -s /usr/local/lib/libinotifytools.so.0 /usr/lib/libinotifytools.so.0

64位系统:ln -s /usr/local/lib/libinotifytools.so.0 /usr/lib64/libinotifytools.so.0

inotifywait命令使用

#!/bin/bash

#filename watchdir.sh

path=$1

/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y/%H:%M' --format '%T %w %f' -e modify,delete,create,attrib $path

执行输出:

./watchdir.sh /data/wsdata/tools/

04/01/17/16:34 /data/wsdata/tools/ .j.jsp.swp

04/01/17/16:34 /data/wsdata/tools/ .j.jsp.swx

04/01/17/16:34 /data/wsdata/tools/ .j.jsp.swx

04/01/17/16:34 /data/wsdata/tools/ .j.jsp.swp

04/01/17/16:34 /data/wsdata/tools/ .j.jsp.swp

04/01/17/16:34 /data/wsdata/tools/ .j.jsp.swp

04/01/17/16:34 /data/wsdata/tools/ .j.jsp.swp

04/01/17/16:34 /data/wsdata/tools/ .j.jsp.swp

04/01/17/16:35 /data/wsdata/tools/ 4913

04/01/17/16:35 /data/wsdata/tools/ 4913

04/01/17/16:35 /data/wsdata/tools/ 4913

04/01/17/16:35 /data/wsdata/tools/ j.jsp

04/01/17/16:35 /data/wsdata/tools/ j.jsp

04/01/17/16:35 /data/wsdata/tools/ j.jsp

04/01/17/16:35 /data/wsdata/tools/ j.jsp~

04/01/17/16:35 /data/wsdata/tools/ .j.jsp.swp

inotifywait命令参数

-m是要持续监视变化。

-r使用递归形式监视目录。

-q减少冗余信息,只打印出需要的信息。

-e指定要监视的事件列表。

--timefmt是指定时间的输出格式。

--format指定文件变化的详细信息。

可监听的事件事件

描述

access

访问,读取文件。

modify

修改,文件内容被修改。

attrib

属性,文件元数据被修改。

move

移动,对文件进行移动操作。

create

创建,生成新文件

open

打开,对文件进行打开操作。

close

关闭,对文件进行关闭操作。

delete

删除,文件被删除。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值