inotify的安装

查看当前系统是否支持inotify

[root@localhost ~]# ls -l /proc/sys/fs/inotify
总用量 0
-rw-r--r-- 1 root root 0 6月  11 09:44 max_queued_events
-rw-r--r-- 1 root root 0 6月  11 09:44 max_user_instances
-rw-r--r-- 1 root root 0 6月  11 09:44 max_user_watches

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

如果出现上面的输出,则说明系统支持
inofity-tools下载地址:http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
上传到自己的虚拟机相关目录

[root@localhost hbk]# ll -h |grep inotify
-rw-r--r--  1 root       root       351K 6月  11 09:58 inotify-tools-3.14.tar.gz
[root@localhost hbk]# pwd
/root/hbk
  • 1
  • 2
  • 3
  • 4

解压

[root@localhost hbk]# tar -zxvf inotify-tools-3.14.tar.gz
  • 1

进行编译安装

[root@localhost hbk]# cd inotify-tools-3.14
[root@localhost inotify-tools-3.14]# ./configure
[root@localhost inotify-tools-3.14]# make && make install
  • 1
  • 2
  • 3

默认安装在/usr/local/bin目录,一共两个程序
inotifywait和inotifywatch

这样就算安装成功了。
inotiry的简单使用介绍,这个工具一般用来监控文件目录的变动,新建修改删除,属性变动等,inotify能够接受到相关信息,并进行日志输出,可以配合rsync做到服务器文件实时同步。

[root@localhost bin]# /usr/local/bin/inotifywait --help
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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64

英语水平好点的,相信看上面的帮助文档毫无压力,接下来讲点常用的知识点。
附一个中文翻译的
在这里插入图片描述

监控事件

为了实验,我们监控/root/hbk/test目录,test为新建的空目录,我们监控这个目录的新建文件,重命名文件,删除文件,修改文件等操作
如下我们监控新建和删除事件

[root@localhost test]# /usr/local/bin/inotifywait -mrq --format '%Xe %w%f' -e create,delete /root/hbk/test
  • 1

执行上面命令,处于阻塞状态,在后台运行,需要另开一个Shell窗口对/root/hbk/test新建目录,文件删除等操作

[root@localhost test]# mkdir images
[root@localhost test]# touch index.html
[root@localhost test]# rm -rf index.html 
  • 1
  • 2
  • 3

可以看到日志输出如下:

CREATEXISDIR /root/hbk/test/images
CREATE /root/hbk/test/index.html
DELETE /root/hbk/test/index.html
  • 1
  • 2
  • 3

我们可以编写一个循环,从日志输出中使用awk取到文件完整路径,然后配合rsync命令做同步,实现文件的实时同步。

inotifywatch命令
命令格式:

inotifywatch  [-hvzrqf] [-e <event> ] [-t <seconds> ] [-a <event> ] [-d <event> ] <file> [ ...]
  • 1

在这里插入图片描述
举例:
统计一小时内/root/hbk/test目录下发生的事件。

[root@localhost test]# /usr/local/bin/inotifywatch -v -t 3600 -r /root/hbk/test

  • 1
  • 2

注意秒数不支持计算,如下是错误的

[root@localhost test]# /usr/local/bin/inotifywatch -v -t 60*60 -r /root/hbk/test
'60*60' is not a valid timeout value.

  • 1
  • 2
  • 3

评论 (2)

  • infilky Reply

    Aug 03 2021 01:07 pm
  • neeries Reply

    涉及广告推广,审核未通过

    Dec 11 2020 01:56 am

发表评论