Hatena::ブログ(Diary)

ymkoの日記

2010-04-29

ファイル、ディレクトリの変更を監視する

$ wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
$ tar xzvf inotify-tools-3.14.tar.gz
$ cd inotify-tools-3.14/
$ ./configure
$ make

別にmake install しなくても使える。

inotifywait と inotifywatch ができる.
$ ls src/
inotifywait inotifywatch

$ man man/inotifywait.1
 -r 再帰的にディレクトリをチェック
 -m Ctrl+Cするまで終わらない。(これを入れないと一回の変更発見で終わる)
EXAMPLEに使い方が書いてある(丁寧だ。。)


'/'以下すべてをチェック対象にかけると・・・
$ ./src/inotifywait -m -r /
Setting up watches.  Beware: since -r was given, this may take a while!
Failed to watch /; upper limit on inotify watches reached!
Please increase the amount of inotify watches allowed per user via `/proc/sys/fs/inotify/max_user_watches'.

一度に監視できるファイル数に制限がある
$ cat /proc/sys/fs/inotify/max_user_watches
8192

'/tmp'を監視対象に
$ ./src/inotifywait -m -r /tmp/
/tmp/ OPEN,ISDIR                      ls /tmp
/tmp/ CLOSE_NOWRITE,CLOSE,ISDIR
/tmp/ CREATE aaa.txt                  touch /tmp/aaa.txt
/tmp/ OPEN aaa.txt 
/tmp/ ATTRIB aaa.txt
/tmp/ CLOSE_WRITE,CLOSE aaa.txt
/tmp/ MODIFY aaa.txt                  echo "123" > /tmp/aaa.txt
/tmp/ OPEN aaa.txt
/tmp/ MODIFY aaa.txt
/tmp/ CLOSE_WRITE,CLOSE aaa.txt

$ wget wget http://search.cpan.org/CPAN/authors/id/M/ML/MLEHMANN/Linux-Inotify2-1.21.tar.gz
$ tar xzvf Linux-Inotify2-1.21.tar.gz
$ cd Linux-Inotify2-1.21/
$ perl Makefile.PL
Checking if your kit is complete...
Looks good
Warning: prerequisite common::sense 0 not found.
Writing Makefile for Linux::Inotify2

common::sence とかいうのがいるらしい。(あとで調べる)

$ wget http://search.cpan.org/CPAN/authors/id/M/ML/MLEHMANN/common-sense-3.2.tar.gz
$ tar xzvf common-sense-3.2.tar.gz
$ perl Makefile.PL
$ make
$ make install

$ cd Linux-Inotify2-1.21/
$ perl Makefile.PL
$ make 
$ make install

あとで書く・・・