Ubuntuでディレクトリを監視する

inotifyを使ってディレクトリの状態を監視する方法。何か面白いことが出来そう。
inotifyをインストールする
$ sudo apt-get install inotify-tools
スクリプトを書く
Apache2のログディレクトリのmodifyイベントを監視して変更があった時にdateコマンドを実行するサンプル。
$ cd /home
$ sudo nano watch.sh
watch.shの内容while inotifywait -e modify /var/log/apache2; do
  date;
done
実行権限を与える。
$ sudo chmod 777 watch.sh
実行するとこんな感じ。
$ ./watch.shSetting up watches.
Watches established.
/var/log/apache2/ MODIFY access.log
Tue Nov  8 12:00:58 JST 2016
Setting up watches.
Watches established.
/var/log/apache2/ MODIFY access.log
Tue Nov  8 12:01:05 JST 2016
Setting up watches.
Watches established.
/var/log/apache2/ MODIFY access.log
Tue Nov  8 12:01:13 JST 2016
Setting up watches.
Watches established