2020-06-23
inotify - 維基百科,自由的百科全書
Linux核心 - 維基百科,自由的百科全書
inotify 說明
fswatch 官方網站
各版本
最新版
Linux Kernal 從 2.6.13 開始提供 inotify 模組(子系統),程式設計師可以使用它提供的 function,對文件變化進行監控。
於是有人開發了使用 inotify 機制的工具 fswatch,省去自行開發麻煩,可以直接使用的工具。
CentOS 8.2 安裝後,預設沒有 fswatch 可用,
yum 安裝也沒也提供它。
所以必須手動下載 .tar.gz 安裝,或先安裝 epel-release 讓 yum 可以安裝。
檢查 kernel 版本
[root@localhost ~]# uname -r
4.18.0-193.el8.x86_64
確認是否支援
[root@localhost ~]# ls -lsart /proc/sys/fs/inotify
total 0
0 dr-xr-xr-x. 1 root root 0 Jun 19 13:33 ..
0 -rw-r--r--. 1 root root 0 Jun 19 13:33 max_user_watches
0 dr-xr-xr-x. 1 root root 0 Jun 19 13:33 .
0 -rw-r--r--. 1 root root 0 Jun 19 13:44 max_user_instances
0 -rw-r--r--. 1 root root 0 Jun 19 13:44 max_queued_events
[root@localhost ~]#
或
[root@localhost ~]# grep INOTIFY_USER /boot/config-$(uname -r)
CONFIG_INOTIFY_USER=y
或
[root@localhost ~]# cat /boot/config-4.18.0-193.el8.x86_64 | grep INOTIFY_USER
CONFIG_INOTIFY_USER=y
開始下載、編譯、安裝
yum 不提供
[root@localhost ~]# yum list | grep fswatch
[root@localhost ~]#
[root@localhost ~]# wget https://github.com/emcrisostomo/fswatch/releases/download/1.14.0/fswatch-1.14.0.tar.gz
[root@localhost ~]# tar -zvxf fswatch-1.14.0.tar.gz
[root@localhost ~]# cd fswatch-1.14.0
[root@localhost fswatch-1.14.0]# yum -y install gcc make gcc-c++
[root@localhost fswatch-1.14.0]# ./configure --prefix=/usr/local/fswatch
[root@localhost fswatch-1.14.0]# make
[root@localhost inotify-tools-3.20.2.2]# make install
若遇到下面錯誤
configure: error: *** A compiler with support for C++11 language features is required.
需要 yum -y install gcc-c++
參數
[root@localhost ~]# /usr/local/fswatch/bin/fswatch -h
fswatch 1.14.0
Usage:
fswatch [OPTION] ... path ...
Options:
-0, --print0 Use the ASCII NUL character (0) as line separator.
-1, --one-event Exit fswatch after the first set of events is received.
--allow-overflow Allow a monitor to overflow and report it as a change event.
--batch-marker Print a marker at the end of every batch.
-a, --access Watch file accesses.
-d, --directories Watch directories only.
-e, --exclude=REGEX Exclude paths matching REGEX.
-E, --extended Use extended regular expressions.
--filter-from=FILE
Load filters from file.
--format=FORMAT Use the specified record format.
-f, --format-time Print the event time using the specified format.
--fire-idle-event Fire idle events.
-h, --help Show this message.
-i, --include=REGEX Include paths matching REGEX.
-I, --insensitive Use case insensitive regular expressions.
-l, --latency=DOUBLE Set the latency.
-L, --follow-links Follow symbolic links.
-M, --list-monitors List the available monitors.
-m, --monitor=NAME Use the specified monitor.
--monitor-property name=value
Define the specified property.
-n, --numeric Print a numeric event mask.
-o, --one-per-batch Print a single message with the number of change events.
-r, --recursive Recurse subdirectories.
-t, --timestamp Print the event timestamp.
-u, --utc-time Print the event time as UTC time.
-x, --event-flags Print the event flags.
--event=TYPE Filter the event by the specified type.
--event-flag-separator=STRING
Print event flags using the specified separator.
-v, --verbose Print verbose output.
--version Print the version of fswatch and exit.
Available monitors in this platform:
inotify_monitor
poll_monitor
See the man page for more information.
Report bugs to <enrico.m.crisostomo@gmail.com>.
fswatch home page: <https://github.com/emcrisostomo/fswatch>.
[root@localhost ~]#
測試監控 /var/www/html 目錄
[root@localhost ~]# /usr/local/fswatch/bin/fswatch -t -d /var/www/html &
[root@localhost ~]# touch /var/www/html/2.html
ue 23 Jun 2020 09:44:53 AM CST /var/www/html/2.htm
Tue 23 Jun 2020 09:44:53 AM CST /var/www/html/2.htm
Tue 23 Jun 2020 09:44:53 AM CST /var/www/html/2.htm
Tue 23 Jun 2020 09:44:53 AM CST /var/www/html/2.htm
輸出到 log 檔案
[root@localhost ~]# /usr/local/fswatch/bin/fswatch -t -d /var/www/html >> /var/log/fswatch.log &
(完)
相關
FileSystemWatcher 類別 (這裡有程式範例)
Java's WatchDir example - Oracle Docs (這裡有程式範例)
[研究] inotify-tools 免費目錄即時監控工具試用 (CentOS 8.2)
[研究] Microsoft FileMon / Process Monitor v3.53 免費目錄即時監控軟體-試用
[研究] FolderChangesView 2.3.1 免費目錄即時監控軟體-試用
[研究] Directory Monitor 2.13.5.0 (27 May 2020) 免費目錄即時監控軟體-試用
沒有留言:
張貼留言