一、inotify简介
inotify是Linux内核的一个功能,它能监控文件系统的变化,比如删除、读、写和卸载等操作。它监控到这些事件的发生后会默认往标准输出打印事件信息。要使用inotify,Linux内核版本必须是大于2.6.13,在这之前的内核都没有这个功能。我们可以利用inotify去监控我们要监控的资源,当资源发生变化时,我们扑捉它的事件,从而触发同步数据等操作。
二、inotify软件介绍
inotify是内核的一个功能,众所周知内核的功能我们必须要配合工具才能使用,通常情况下用户要使用内核的功能,都需要用户空间的软件去调用才可以达到使用内核的功能的目的,用户是无法直接操内核的。实现inotify软件有inotify-tools、sersync、lrsyncd。我们这里以inotify-tools这个软件包为例进行实验;inotify-tools包主要有两个文件,一个是inotifywait: 在被监控的文件或目录上等待特定文件系统事件(open close delete等)发生,常用于实时同步的目录监控;一个是inotifywatch:收集被监控的文件系统使用的统计数据,指文件系统事件发生的次数统计。通常情况下我们使用iontifywait就可以了。接下来我们来安装inotify-tools
三、inotify软件安装和使用
首先我们要确认我们手上的Linux系统是否支持inotify这个功能
[root@test ~]# uname -r
3.10.0-957.27.2.el7.x86_64
[root@test ~]# ll /proc/sys/fs/inotify/
总用量 0
-rw-r--r-- 1 root root 0 1月 31 16:29 max_queued_events
-rw-r--r-- 1 root root 0 1月 31 16:29 max_user_instances
-rw-r--r-- 1 root root 0 1月 31 16:29 max_user_watches
[root@test ~]#
提示:如果我们的内核版本高于2.6.13,并且有/proc/sys/fs/inotify/目录,说明我们的系统上支持inotify功能的
inotify内核参数说明
我们在/proc/sys/fs/inotify这个目录下看到有三个文件,这三个文件就是记录inotify的内核参数的文件。其中max_queued_events这个文件记录inotify事件队列最大长度,如值太小会报Event Queue Overflow 错误,默认值:16384,当然这个值是可以更改的,临时更改可以用echo 命令加上值给重定向到这个文件中。永久修改需要修改编辑/etc/sysctl.conf
[root@test ~]# cat /proc/sys/fs/inotify/max_queued_events
16384
[root@test ~]# echo 'fs.inotify.max_queued_events=999999' >> /etc/sysctl.conf
[root@test ~]# cat /etc/sysctl.conf
# sysctl settings are defined through files in
# /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
#
# Vendors settings live in /usr/lib/sysctl.d/.
# To override a whole file, create a new file with the same in
# /etc/sysctl.d/ and put new settings there. To override
# only specific settings, add a file with a lexically later
# name in /etc/sysctl.d/ and put new settings there.
#
# For more information, see sysctl.conf(5) and sysctl.d(5).
fs.inotify.max_queued_events=999999
[root@test ~]# sysctl -p
fs.inotify.max_queued_events = 999999
[root@test ~]# cat /proc/sys/fs/inotify/max_queued_events
999999
[root@test ~]#
提示:修改内核其他参数,也是是类似的方法。
max_user_watches:这个文件主要存放单个进程监控文件的数量,默认是8129;生产环境中如果监控的文件数量庞大,可以通过上面的方式去修改这个值
max_user_instances:这个文件存放用户创建inotify实例的最大值,默认是128;同样也是可以通过上面的方式修改
inotify软件安装
参考文档https://github.com/rvoicilas/inotify-tools/wiki
inotify是基于epel源,在用yum安装前请先配好yum源
[root@test ~]# yum info inotify-tools
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
可安装的软件包
名称 :inotify-tools
架构 :x86_64
版本 :3.14
发布 :9.el7
大小 :51 k
源 :epel/x86_64
简介 : Command line utilities for inotify
网址 :http://inotify-tools.sourceforge.net/
协议 : GPLv2
描述 : inotify-tools is a set of command-line programs for Linux providing
: a simple interface to inotify. These programs can be used to monitor
: and act upon filesystem events.
[root@test ~]#
提示:如果用yum可以看到inotify-tools的说明信息,说明你的Linux yum仓库里有这个软件包,可以直接yum 安装即可
[root@test ~]# yum install -y inotify-tools
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
base | 3.6 kB 00:00:00
dockerrepo | 2.9 kB 00:00:00
epel | 5.3 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
updates/7/x86_64/primary_db | 5.9 MB 00:00:01
正在解决依赖关系
--> 正在检查事务
---> 软件包 inotify-tools.x86_64.0.3.14-9.el7 将被 安装
--> 解决依赖关系完成
依赖关系解决
=================================================================================================================================
Package 架构 版本 源 大小
=================================================================================================================================
正在安装:
inotify-tools x86_64 3.14-9.el7 epel 51 k
事务概要
========================================================================================================&