mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-08 23:14:06 +08:00
158 lines
6.2 KiB
HTML
158 lines
6.2 KiB
HTML
<h1 id="inotifywait">inotifywait</h1>
|
||
<p>异步文件系统监控机制</p>
|
||
<h2 id="补充说明">补充说明</h2>
|
||
<p><strong>Inotify</strong> 一种强大的、细粒度的、异步文件系统监控机制,它满足各种各样的文件监控需要,可以监控文件系统的访问属性、读写属性、权限属性、删除创建、移动等操作,也就是可以监控文件发生的一切变化。。</p>
|
||
<p><strong>inotify-tools</strong> 是一个C库和一组命令行的工作提供Linux下inotify的简单接口。inotify-tools安装后会得到<code>inotifywait</code>和<code>inotifywatch</code>这两条命令:</p>
|
||
<ul>
|
||
<li><strong>inotifywait命令</strong> 可以用来收集有关文件访问信息,Linux发行版一般没有包括这个命令,需要安装inotify-tools,这个命令还需要将inotify支持编译入Linux内核,好在大多数Linux发行版都在内核中启用了inotify。</li>
|
||
<li><strong>inotifywatch命令</strong> 用于收集关于被监视的文件系统的统计数据,包括每个 inotify 事件发生多少次。</li>
|
||
</ul>
|
||
<p>开始之前需要检测系统内核是否支持inotify:</p>
|
||
<p>使用<code>uname -r</code>命令检查Linux内核,如果低于2.6.13,就需要重新编译内核加入inotify的支持。</p>
|
||
<p>使用<code>ll /proc/sys/fs/inotify</code>命令,是否有以下三条信息输出,如果没有表示不支持。</p>
|
||
<pre><code class="language-bash">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
|
||
</code></pre>
|
||
<h3 id="安装inotify-tools">安装inotify-tools</h3>
|
||
<ul>
|
||
<li>inotify-tools项目地址:https://github.com/rvoicilas/inotify-tools</li>
|
||
<li>inotify-tools下载地址:http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz</li>
|
||
</ul>
|
||
<pre><code class="language-bash">#CentOS release 5.8/64位:
|
||
tar zxvf inotify-tools-3.14.tar.gz
|
||
cd inotify-tools-3.14
|
||
./configure
|
||
make
|
||
make install</code></pre>
|
||
<p>其他Linux发行版安装方法可以参见:https://github.com/rvoicilas/inotify-tools/wiki#wiki-getting</p>
|
||
<h3 id="inotify相关参数">inotify相关参数</h3>
|
||
<p>inotify定义了下列的接口参数,可以用来限制inotify消耗kernel memory的大小。由于这些参数都是内存参数,因此,可以根据应用需求,实时的调节其大小:</p>
|
||
<ul>
|
||
<li><code>/proc/sys/fs/inotify/max_queued_evnets</code>表示调用inotify_init时分配给inotify instance中可排队的event的数目的最大值,超出这个值的事件被丢弃,但会触发IN_Q_OVERFLOW事件。</li>
|
||
<li><code>/proc/sys/fs/inotify/max_user_instances</code>表示每一个real user id可创建的inotify instatnces的数量上限。</li>
|
||
<li><code>/proc/sys/fs/inotify/max_user_watches</code>表示每个inotify instatnces可监控的最大目录数量。如果监控的文件数目巨大,需要根据情况,适当增加此值的大小。</li>
|
||
</ul>
|
||
<p>根据以上在32位或者64位系统都可以执行:</p>
|
||
<pre><code class="language-bash">echo 104857600 > /proc/sys/fs/inotify/max_user_watches
|
||
echo 'echo 104857600 > /proc/sys/fs/inotify/max_user_watches' >> /etc/rc.local</code></pre>
|
||
<p>如果遇到以下错误:</p>
|
||
<pre><code class="language-bash">inotifywait: error while loading shared libraries: libinotifytools.so.0: cannot open shared object file: No such file or directory </code></pre>
|
||
<pre><code class="language-bash"> **解决方法:**
|
||
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</code></pre>
|
||
<h3 id="inotifywait命令使用">inotifywait命令使用</h3>
|
||
<pre><code class="language-bash">#!/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/13/16:34 /data/wsdata/tools/ .j.jsp.swp
|
||
04/01/13/16:34 /data/wsdata/tools/ .j.jsp.swx
|
||
04/01/13/16:34 /data/wsdata/tools/ .j.jsp.swx
|
||
04/01/13/16:34 /data/wsdata/tools/ .j.jsp.swp
|
||
04/01/13/16:34 /data/wsdata/tools/ .j.jsp.swp
|
||
04/01/13/16:34 /data/wsdata/tools/ .j.jsp.swp
|
||
04/01/13/16:34 /data/wsdata/tools/ .j.jsp.swp
|
||
04/01/13/16:34 /data/wsdata/tools/ .j.jsp.swp
|
||
04/01/13/16:35 /data/wsdata/tools/ 4913
|
||
04/01/13/16:35 /data/wsdata/tools/ 4913
|
||
04/01/13/16:35 /data/wsdata/tools/ 4913
|
||
04/01/13/16:35 /data/wsdata/tools/ j.jsp
|
||
04/01/13/16:35 /data/wsdata/tools/ j.jsp
|
||
04/01/13/16:35 /data/wsdata/tools/ j.jsp
|
||
04/01/13/16:35 /data/wsdata/tools/ j.jsp~
|
||
04/01/13/16:35 /data/wsdata/tools/ .j.jsp.swp
|
||
</code></pre>
|
||
<h3 id="inotifywait命令参数">inotifywait命令参数</h3>
|
||
<ul>
|
||
<li><code>-m</code>是要持续监视变化。</li>
|
||
<li><code>-r</code>使用递归形式监视目录。</li>
|
||
<li><code>-q</code>减少冗余信息,只打印出需要的信息。</li>
|
||
<li><code>-e</code>指定要监视的事件列表。</li>
|
||
<li><code>--timefmt</code>是指定时间的输出格式。</li>
|
||
<li><code>--format</code>指定文件变化的详细信息。</li>
|
||
</ul>
|
||
<h4 id="可监听的事件">可监听的事件</h4>
|
||
<table border="0" height="193" style="width: 100%;" width="74">
|
||
<tbody>
|
||
<tr>
|
||
<th>
|
||
事件
|
||
</th>
|
||
<th>
|
||
描述
|
||
</th>
|
||
</tr>
|
||
<tr>
|
||
<td>
|
||
access
|
||
</td>
|
||
<td>
|
||
<strong>访问</strong> ,读取文件。
|
||
</td>
|
||
</tr>
|
||
<tr>
|
||
<td>
|
||
modify
|
||
</td>
|
||
<td>
|
||
<strong>修改</strong> ,文件内容被修改。
|
||
</td>
|
||
</tr>
|
||
<tr>
|
||
<td>
|
||
attrib
|
||
</td>
|
||
<td>
|
||
<strong>属性</strong> ,文件元数据被修改。
|
||
</td>
|
||
</tr>
|
||
<tr>
|
||
<td>
|
||
move
|
||
</td>
|
||
<td>
|
||
<strong>移动</strong> ,对文件进行移动操作。
|
||
</td>
|
||
</tr>
|
||
<tr>
|
||
<td>
|
||
create
|
||
</td>
|
||
<td>
|
||
<strong>创建</strong> ,生成新文件
|
||
</td>
|
||
</tr>
|
||
<tr>
|
||
<td>
|
||
open
|
||
</td>
|
||
<td>
|
||
<strong>打开</strong> ,对文件进行打开操作。
|
||
</td>
|
||
</tr>
|
||
<tr>
|
||
<td>
|
||
close
|
||
</td>
|
||
<td>
|
||
<strong>关闭</strong> ,对文件进行关闭操作。
|
||
</td>
|
||
</tr>
|
||
<tr>
|
||
<td>
|
||
delete
|
||
</td>
|
||
<td>
|
||
<strong>删除</strong> ,文件被删除。
|
||
</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
<!-- Linux命令行搜索引擎:https://jaywcjlove.github.io/linux-command/ -->
|