文章目录

[隐藏]

Linux内核从2.6.13开始,引入了inotify机制。通过intofity机制,能够对文件系统的变化进行监控,如对文件进行创建、删除、修改等操作,可以及时通知应用程序进行相关事件的处理。这种响应处理机制,避免了频繁的文件轮询任务,提高了任务的处理效率。

一、检查系统内核版本

[root@localhost tan]# uname -r
2.6.32-696.el6.x86_64

二、检查系统是否支持inotify

[root@localhost tan]# ls -lsart /proc/sys/fs/inotify
total 0
0 dr-xr-xr-x 0 root root 0 Jan 17 08:47 ..
0 dr-xr-xr-x 0 root root 0 Jan 17 08:53 .
0 -rw-r--r-- 1 root root 0 Jan 19 13:33 max_user_watches
0 -rw-r--r-- 1 root root 0 Jan 19 13:33 max_queued_events
0 -rw-r--r-- 1 root root 0 Jan 19 13:40 max_user_instances
[root@localhost tan]#

如果出现上面结果说明系统支持inotify。

三、下载安装(下载有点慢)

[root@localhost tan]#wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz  

[root@localhost tan]# tar -zvxf inotify-tools-3.14.tar.gz
[root@localhost tan]# cd inotify-tools-3.14 [root@localhost inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify
[root@localhost inotify-tools-3.14]# make
[root@localhost inotify-tools-3.14]# make install

四、查看inotify默认参数

[root@localhost bin]# sysctl -a | grep max_queued_events
fs.inotify.max_queued_events = 16384 [root@localhost bin]# sysctl -a | grep max_user_watches
fs.inotify.max_user_watches = 8192
fs.epoll.max_user_watches = 798863 [root@localhost bin]# sysctl -a | grep max_user_instances
fs.inotify.max_user_instances = 128

五、修改inotify参数

1、命令修改

[root@localhost bin]# sysctl -w fs.inotify.max_user_instances=130
fs.inotify.max_user_instances = 130

2、文件修改

[root@localhost]# vi /etc/sysctl.conf
#添加如下代码
fs.inotify.max_user_instances=130

3、参数说明

  • max_user_instances:每个用户创建inotify实例最大值
  • max_queued_events:inotify队列最大长度,如果值太小,会出现错误,导致监控文件不准确
  • max_user_watches:要知道同步的文件包含的目录数,可以用:
    [root@localhost]# find /home/rain -type d|wc -l 统计,必须保证参数值大于统计结果(/home/tan/uploadFile/为同步文件目录)。

六、创建实时监控脚本 (file 里面放的需要监听的目录)

[root@localhost shell]# vi inotify.sh
/usr/local/inotify/bin/inotifywait -mrq -e modify,create,move,delete --fromfile '/root/shell/file' --timefmt '%y-%m-%d %H:%M' --format '%T %w%f %e' --outfile '/home/tan/inotify.log'
[root@localhost shell]# vi file 

/home/tan
@/home/tan/uploadFile

inotifywait常用参数:

  • –timefmt 时间格式
  • %y年 %m月 %d日 %H小时 %M分钟
  • –format 输出格式
  • %T时间 %w路径 %f文件名 %e状态
  • -m 始终保持监听状态,默认触发事件即退出。
  • -r 递归查询目录
  • -q 打印出监控事件
  • -e 定义监控的事件,可用参数:
  • open 打开文件
  • access 访问文件
  • modify 修改文件
  • delete 删除文件
  • create 新建文件
  • attrb 属性变更
事件  描述

access  访问,读取文件。
modify 修改,文件内容被修改。
attrib 属性,文件元数据被修改。
move 移动,对文件进行移动操作。
create 创建,生成新文件
open 打开,对文件进行打开操作。
close 关闭,对文件进行关闭操作。
delete 删除,文件被删除。

七:实例操作

1、首先启动监听脚本,权限问题的话先:chmod 755 inotify.sh

[root@localhost shell]# ./inotify.sh

我在监听的目录中上传文件,没截图,自己想象吧

[root@localhost tan]# cat inotify.log
18-01-19 15:07 /home/tan/uploadfile/test//ssh.txt CREATE
18-01-19 15:13 /home/tan/uploadfile/test/QQ20180119143826.png CREATE

这里可以看到打印的日志了。

八、附

1、inotifywait

使用方法和参数说明: 使用命令help就行

[root@localhost bin]# ./inotifywait -h
inotifywait 3.14
Wait for a particular event on a file or set of files.
Usage: inotifywait [ options ] file1 [ file2 ] [ file3 ] [ ... ]
Options:
-h|--help Show this help text.
@<file> Exclude the specified file from being watched.
--exclude <pattern>
Exclude all events on files matching the
extended regular expression <pattern>.
--excludei <pattern>
Like --exclude but case insensitive.
-m|--monitor Keep listening for events forever. Without
this option, inotifywait will exit after one
event is received.
-d|--daemon Same as --monitor, except run in the background
logging events to a file specified by --outfile.
Implies --syslog.
-r|--recursive Watch directories recursively.
--fromfile <file>
Read files to watch from <file> or `-' for stdin.
-o|--outfile <file>
Print events to <file> rather than stdout.
-s|--syslog Send errors to syslog rather than stderr.
-q|--quiet Print less (only print events).
-qq Print nothing (not even events).
--format <fmt> Print using a specified printf-like format
string; read the man page for more details.
--timefmt <fmt> strftime-compatible format string for use with
%T in --format string.
-c|--csv Print events in CSV format.
-t|--timeout <seconds>
When listening for a single event, time out after
waiting for an event for <seconds> seconds.
If <seconds> is 0, inotifywait will never time out.
-e|--event <event1> [ -e|--event <event2> ... ]
Listen for specific event(s). If omitted, all events are
listened for. Exit status:
0 - An event you asked to watch for was received.
1 - An event you did not ask to watch for was received
(usually delete_self or unmount), or some error occurred.
2 - The --timeout option was given and no events occurred
in the specified interval of time. 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
writeable 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、inotifywatch

使用方法和参数说明:

[root@localhost bin]# ./inotifywatch -h
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.
@<file> Exclude the specified file from being watched.
--fromfile <file>
Read files to watch from <file> or `-' for stdin.
--exclude <pattern>
Exclude all events on files matching the extended regular
expression <pattern>.
--excludei <pattern>
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 <seconds>
Listen only for specified amount of time in seconds; if
omitted or 0, inotifywatch will execute until receiving an
interrupt signal.
-e|--event <event1> [ -e|--event <event2> ... ]
Listen for specific event(s). If omitted, all events are
listened for.
-a|--ascending <event>
Sort ascending by a particular event, or `total'.
-d|--descending <event>
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
writeable 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   常用事件
  1. CREATE,创建
  2. MODIFY,修改
  3. CLOSE_WRITE,CLOSE,写入成功

原文出处:1felse -> http://www.1felse.com/blog/view.do?blogId=ff8080816102bdbb01610d45232d005a

CentOS 6.9上inotify-tools 安装及使用方法的更多相关文章

  1. 四、CentOS 6.5 上传和安装Nginx

    CentOS 6.5 上传和安装Nginx

  2. 【转】【CentOS】【Python】Centos7安装Python3的方法

    由于centos7原本就安装了Python2,而且这个Python2不能被删除,因为有很多系统命令,比如yum都要用到. [root@VM_105_217_centos Python-3.6.2]# ...

  3. 在VMware Workstation 16上安装Windows7虚拟机以及VMware tools安装失败解决方法

    安装VMware Workstation 16 搜素"VMware Workstation下载" 下载 VMware Workstation Pro 下载Windows7系统镜像 ...

  4. 在Linux CentOS上编译并安装Clang 3.5.0

    编译CoreCLR需要Clang 3.5,而CentOS上安装的是Clang 3.4.2(yum repos中最新版也是这个),只能自己手工编译LLVM的源代码进行安装. (注:CentOS的版本是6 ...

  5. [转]在Linux CentOS 6.6上安装Python 2.7.9

    在Linux CentOS 6.6上安装Python 2.7.9 查看python安装版本 python -V yum中最新的也是Python 2.6.6,所以只能下载Python 2.7.9的源代码 ...

  6. 如何在Ubuntu / CentOS 6.x上安装Bugzilla 4.4

    这里,我们将展示如何在一台Ubuntu 14.04或CentOS 6.5/7上安装Bugzilla.Bugzilla是一款基于web,用来记录跟踪缺陷数据库的bug跟踪软件,它同时是一款免费及开源软件 ...

  7. 如何在 centos 7.3 上安装 caffe 深度学习工具

    有好多朋友在安装 caffe 时遇到不少问题.(看文章的朋友希望关心一下我的创业项目趣智思成) 今天测试并整理一下安装过程.我是在阿里云上测试,选择centos 7.3 镜像. 先安装 epel 源 ...

  8. 在CentOS或RHEL上安装Nux Dextop仓库

    介绍 Nux Dextop是类似CentOS.RHEL.ScientificLinux的第三方RPM仓库(比如:Ardour,Shutter等等).目前,Nux Dextop对CentOS/RHEL ...

  9. 怎样在CentOS 7.0上安装和配置VNC服务器

    VNC轻松连接远程Linux桌面 http://jingyan.baidu.com/article/6c67b1d6f1bac92786bb1e6d.html 这是一个关于怎样在你的 CentOS 7 ...

  10. CentOS 6.5上安装MySQL-Cluster

    参考博文: CentOS 6.2下MySQL Cluster 7.2 配置数据库集群 CentOS 6.5上安装MySQL-Cluster 7.3.4过程笔记--下一步学习 MySQL Cluster ...

随机推荐

  1. 1033 To Fill or Not to Fill

    PAT A 1033 To Fill or Not to Fill With highways available, driving a car from Hangzhou to any other ...

  2. 20169207《Linux内核原理与分析》第九周作业

    第九周的实验依旧和往常的一样,主要包括两部分.一是1.阅读学习教材「Linux内核设计与实现 (Linux Kernel Development)」第教材第13,14章.二是学习MOOC「Linux内 ...

  3. CentOS 7通过Firewall开放防火墙端口

    发现在CentOS 7上开放端口用iptables没效果(或者是sodino没找到正确的命令,传说Centos7 下默认的防火墙是 Firewall,替代了之前的 iptables)… 使用firew ...

  4. JS学习笔记2_面向对象

    1.对象的定义 ECMAScript中,对象是一个无序属性集,这里的“属性”可以是基本值.对象或者函数 2.数据属性与访问器属性 数据属性即有值的属性,可以设置属性只读.不可删除.不可枚举等等 访问器 ...

  5. .Net Core Razor 预编译,动态编译,混合编译

    预编译 预编译是ASP .Net Core的默认方式.在发布时,默认会将系统中的所有Razor视图进行预编译.编译好的视图DLL统一命名为 xxx.PrecompiledViews.dll 或者 xx ...

  6. PowerDesigner的Name和Code不同步设置

    1.“Tools”->"GeneralOptions"(最下方) 2.“Dialog”(左侧列表选第2个) 3.“Name to Code mirroring”的勾去掉

  7. lua编程之lua与C相互调用

    lua是扩展性非常良好的语言,虽然核心非常精简,但是用户可以依靠lua库来实现大部分工作.除此之外,lua还可以通过与C函数相互调用来扩展程序功能.在C中嵌入lua脚本既可以让用户在不重新编译代码的情 ...

  8. 【文文殿下】P3740 [HAOI2014]贴海报

    题解 一开始想到离散化,然后暴力模拟.但是存在一种hack数据: [5,7] [1,5] [7,9] 这样会错误的认为第一个区间被覆盖了(因为两个端点被覆盖).所以我们设置一个玄学调参系数,在一个区间 ...

  9. Linux启动流程与模块管理(15)

    系统的启动其实是一项非常复杂的过程,因为内核得要检测硬件并加载适当的驱动程序,接下来则必须要调用程序来准备好系统运行的环境,以让用户能够顺利的操作整台主机系统,如果你能够理解系统启动的原理,那么将有助 ...

  10. Swift5 语言指南(二十七) 访问控制

    访问控制限制从其他源文件和模块中的代码访问部分代码.此功能使您可以隐藏代码的实现细节,并指定一个首选接口,通过该接口可以访问和使用该代码. 您可以为各个类型(类,结构和枚举)以及属于这些类型的属性,方 ...