inotify-tools をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
検索
|
最終更新
|
ヘルプ
|
ログイン
]
開始行:
ファイルシステムへのイベントをチャッチして仕掛けを組める...
本家様 [[https://github.com/rvoicilas/inotify-tools/wiki>...
[[inotify-tools/FileTransfer]]
[[inotify-tools/WindowsPC]]
[[inotify-tools/programming]]
***インストール [#se1ff3be]
CentOS7はepelリポジトリからインストール可能で、epelリポジ...
#code(nonumber){{
[root@c ~]# yum --enablerepo=epel install inotify-tools
}}
インストールされるプログラムは「&color(magenta){/usr/bin/...
***inotifywait [#mece3548]
特定フォルダ、ファイルを監視して通知する
下記の例は /tmp に対してのイベントを監視して通知します。
#code(nonumber){{
[root@c ~]# inotifywait -m -r /tmp -q --format '%T %w%f (...
}}
ファイルを作ってみると(&color(orangered){touch /tmp/sampl...
#code(nonumber){{
2015-06-28 13:28:38 /tmp/sample (CLOSE_WRITE,CLOSE)
}}
と記録される。っが、ファイルのモードの変更(&color(orange)...
#code(nonumber){{
-e|--event <event1> [ -e|--event <event2> ... ]
Listen for specific event(s). If omitted...
listened for.
}}
まぁ、いい。全てを記載してみる。
#code(nonumber){{
[root@c ~]# inotifywait -m -r /tmp -q --format '%T %w%f (...
-e attrib -e close_write -e close_nowrite -e close -e ope...
-e delete -e delete_self -e unmount
}}
そして、ファイルを作ってみる(&color(orangered){touch /tmp...
#code(nonumber){{
2015-06-28 13:44:49 /tmp/sample (CREATE)
2015-06-28 13:44:49 /tmp/sample (OPEN)
2015-06-28 13:44:49 /tmp/sample (ATTRIB)
2015-06-28 13:44:49 /tmp/sample (CLOSE_WRITE,CLOSE)
}}
次に編集してみる(&color(orangered){echo 'This is sample.'...
#code(nonumber){{
2015-06-28 13:46:30 /tmp/sample (MODIFY)
2015-06-28 13:46:30 /tmp/sample (OPEN)
2015-06-28 13:46:30 /tmp/sample (MODIFY)
2015-06-28 13:46:30 /tmp/sample (CLOSE_WRITE,CLOSE)
}}
ファイルモードを変更してみる(&color(orangered){chmod 600 ...
#code(nonumber){{
2015-06-28 13:47:21 /tmp/sample (ATTRIB)
}}
所有者を変更してみる(&color(orangered){chown saber /tmp/s...
#code(nonumber){{
2015-06-28 13:48:11 /tmp/sample (ATTRIB)
}}
削除(&color(orangered){rm /tmp/sample};)
#code(nonumber){{
2015-06-28 13:49:18 /tmp/sample (DELETE)
}}
と一連の操作は記録される。
&color(white,blue){留意};
/home を対象とすると
#code(nonumber){{
Failed to watch /home; upper limit on inotify watches rea...
Please increase the amount of inotify watches allowed per...
}}
と言われる。監視対象が多いとエラーになる。適時&color(crim...
#code(nonumber){{
[root@c ~]# cat /proc/sys/fs/inotify/max_user_watches
8192
[root@c ~]# echo 40960 > /proc/sys/fs/inotify/max_user_wa...
}}
&color(red){*};ただし、監視一つ当たり 1 kB のメモリーが消...
対象としたいパスのディレクトリの数は下記で調べられる。
#code(nonumber){{
[root@c /]# find /home/ -type d -print| wc -l
263712
[root@c /]#
}}
これを元にして、
#code(nonumber){{
[root@c ~]# echo 270000 > /proc/sys/fs/inotify/max_user_w...
[root@c ~]# inotifywait -m -r /home -q --format '%T %w%f ...
}}
とするとエラーは発生しなくなった。ただし270000 -> 270000 ...
参照先:http://unix.stackexchange.com/questions/13751/ker...
***inotifywaitからスクリプトを実行 [#b16a4f4a]
inotifywaitで感知したファイルを引数に、スクリプトを動かし...
inotifywait自体は感知するだけで、後に連携させることはなく...
この提示するファイル名を拾う仕組みが必要みたい。
オプションの「-mrq」は、「-m」はmonitorの意で継続的に監視...
「-r」はrecursiveの意味で指定したフォルダより深い階層も監...
「-q」はquietの意味。指定すると余計なログを出さない
「--format」は書き出す文字列のフォーマットを意味する。「%...
「%w」は監視対象を含めてのフォルダを示す。「%f」はイベン...
#code(bash,nonumber,nooutline){{
#!/bin/bash
inotifywait -mrq --format '%T %w%f (%e)' --timefmt '%F %T...
| while [ 1 ]; do
messages="";
while read -t 0.01 line; do
messages=$line
done
if [ -n "$messages" ]; then
echo $messages
f=`echo $messages|cut -d' ' -f3`
cp $f /share2
fi
done
}}
「read -t 0.1」は、0.1秒間隔で監視します。
inotifywaitはそんな間隔なしにファイルをwatchできるのだが...
終了行:
ファイルシステムへのイベントをチャッチして仕掛けを組める...
本家様 [[https://github.com/rvoicilas/inotify-tools/wiki>...
[[inotify-tools/FileTransfer]]
[[inotify-tools/WindowsPC]]
[[inotify-tools/programming]]
***インストール [#se1ff3be]
CentOS7はepelリポジトリからインストール可能で、epelリポジ...
#code(nonumber){{
[root@c ~]# yum --enablerepo=epel install inotify-tools
}}
インストールされるプログラムは「&color(magenta){/usr/bin/...
***inotifywait [#mece3548]
特定フォルダ、ファイルを監視して通知する
下記の例は /tmp に対してのイベントを監視して通知します。
#code(nonumber){{
[root@c ~]# inotifywait -m -r /tmp -q --format '%T %w%f (...
}}
ファイルを作ってみると(&color(orangered){touch /tmp/sampl...
#code(nonumber){{
2015-06-28 13:28:38 /tmp/sample (CLOSE_WRITE,CLOSE)
}}
と記録される。っが、ファイルのモードの変更(&color(orange)...
#code(nonumber){{
-e|--event <event1> [ -e|--event <event2> ... ]
Listen for specific event(s). If omitted...
listened for.
}}
まぁ、いい。全てを記載してみる。
#code(nonumber){{
[root@c ~]# inotifywait -m -r /tmp -q --format '%T %w%f (...
-e attrib -e close_write -e close_nowrite -e close -e ope...
-e delete -e delete_self -e unmount
}}
そして、ファイルを作ってみる(&color(orangered){touch /tmp...
#code(nonumber){{
2015-06-28 13:44:49 /tmp/sample (CREATE)
2015-06-28 13:44:49 /tmp/sample (OPEN)
2015-06-28 13:44:49 /tmp/sample (ATTRIB)
2015-06-28 13:44:49 /tmp/sample (CLOSE_WRITE,CLOSE)
}}
次に編集してみる(&color(orangered){echo 'This is sample.'...
#code(nonumber){{
2015-06-28 13:46:30 /tmp/sample (MODIFY)
2015-06-28 13:46:30 /tmp/sample (OPEN)
2015-06-28 13:46:30 /tmp/sample (MODIFY)
2015-06-28 13:46:30 /tmp/sample (CLOSE_WRITE,CLOSE)
}}
ファイルモードを変更してみる(&color(orangered){chmod 600 ...
#code(nonumber){{
2015-06-28 13:47:21 /tmp/sample (ATTRIB)
}}
所有者を変更してみる(&color(orangered){chown saber /tmp/s...
#code(nonumber){{
2015-06-28 13:48:11 /tmp/sample (ATTRIB)
}}
削除(&color(orangered){rm /tmp/sample};)
#code(nonumber){{
2015-06-28 13:49:18 /tmp/sample (DELETE)
}}
と一連の操作は記録される。
&color(white,blue){留意};
/home を対象とすると
#code(nonumber){{
Failed to watch /home; upper limit on inotify watches rea...
Please increase the amount of inotify watches allowed per...
}}
と言われる。監視対象が多いとエラーになる。適時&color(crim...
#code(nonumber){{
[root@c ~]# cat /proc/sys/fs/inotify/max_user_watches
8192
[root@c ~]# echo 40960 > /proc/sys/fs/inotify/max_user_wa...
}}
&color(red){*};ただし、監視一つ当たり 1 kB のメモリーが消...
対象としたいパスのディレクトリの数は下記で調べられる。
#code(nonumber){{
[root@c /]# find /home/ -type d -print| wc -l
263712
[root@c /]#
}}
これを元にして、
#code(nonumber){{
[root@c ~]# echo 270000 > /proc/sys/fs/inotify/max_user_w...
[root@c ~]# inotifywait -m -r /home -q --format '%T %w%f ...
}}
とするとエラーは発生しなくなった。ただし270000 -> 270000 ...
参照先:http://unix.stackexchange.com/questions/13751/ker...
***inotifywaitからスクリプトを実行 [#b16a4f4a]
inotifywaitで感知したファイルを引数に、スクリプトを動かし...
inotifywait自体は感知するだけで、後に連携させることはなく...
この提示するファイル名を拾う仕組みが必要みたい。
オプションの「-mrq」は、「-m」はmonitorの意で継続的に監視...
「-r」はrecursiveの意味で指定したフォルダより深い階層も監...
「-q」はquietの意味。指定すると余計なログを出さない
「--format」は書き出す文字列のフォーマットを意味する。「%...
「%w」は監視対象を含めてのフォルダを示す。「%f」はイベン...
#code(bash,nonumber,nooutline){{
#!/bin/bash
inotifywait -mrq --format '%T %w%f (%e)' --timefmt '%F %T...
| while [ 1 ]; do
messages="";
while read -t 0.01 line; do
messages=$line
done
if [ -n "$messages" ]; then
echo $messages
f=`echo $messages|cut -d' ' -f3`
cp $f /share2
fi
done
}}
「read -t 0.1」は、0.1秒間隔で監視します。
inotifywaitはそんな間隔なしにファイルをwatchできるのだが...
ページ名:
1