BLOGTIMES
2009/12/06

Rubyからinotifyを使ってファイルやディレクトリを監視する

  ruby 
このエントリーをはてなブックマークに追加

昨日incronの使い方を調べたのですが、よく考えたらこれってファイルが変更される度に新たなプロセスが生成されるので、マシンの負荷的にあまりよろしくないような気がしてきました。処理が重かったりする事を考えるとなんとか単一の常駐プロセスでやりたいところです。ということで、Rubyからinotifyを使って任意のファイルやディレクトリを監視する方法を調べてみました。

ライブラリのインストール

まず、gemcutterでinotifyを検索してみるといくつかライブラリが見つかります。どれがいいのか良くわからないので、一番枯れていそうなRInotifyを使ってみます。なぜかgemでネットワーク越しにインストールできなかったので、RubyForgeのプロジェクトのページからgemを落としてインストールしました。

# wget http://rubyforge.org/frs/download.php/19778/RInotify-0.9-i586-linux.gem # gem install --local RInotify

rubyで書いてみる

前回と同じログを出力するプログラムはこんな感じになります。これだとファイルが変更される度にプロセスが起動される事もないので、幾分マシンには優しそうです。

/tmp/test1.rb

#!/usr/bin/ruby require 'time' require 'rubygems' require 'rinotify' EVENT_TYPES = RInotify.constants.dup.reject!{|x| x[0,2] != "IN" || x == "IN_ONESHOT" || x == "IN_ALL_EVENTS"} rinotify = RInotify.new rinotify.add_watch("/tmp/test1", RInotify::IN_ALL_EVENTS) trap(:INT) { rinotify.close exit } while true rinotify.each_event do |event| ts = Time.now.localtime.iso8601 trapped = EVENT_TYPES.find_all do |e| event.check_mask(RInotify.const_get(e)) end puts "#{ts},#{rinotify.watch_descriptors[event.watch_descriptor]},#{event.name},#{trapped.join(',')}" end end

先日と同じ処理を走らせたときのログはこんな感じになります。
前回のよりもちょっとイベントが冗長な感じなので、そのあたりは工夫が必要になりそうです。

$ ruby test1.rb 2009-12-06T23:02:43+09:00,/tmp/test1,aa,IN_CREATE 2009-12-06T23:02:43+09:00,/tmp/test1,aa,IN_OPEN 2009-12-06T23:02:43+09:00,/tmp/test1,aa,IN_ATTRIB 2009-12-06T23:02:43+09:00,/tmp/test1,aa,IN_CLOSE_WRITE,IN_CLOSE 2009-12-06T23:02:49+09:00,/tmp/test1,,IN_OPEN,IN_ISDIR 2009-12-06T23:02:49+09:00,/tmp/test1,,IN_ISDIR,IN_CLOSE,IN_CLOSE_NOWRITE 2009-12-06T23:02:49+09:00,/tmp/test1,,IN_OPEN,IN_ISDIR 2009-12-06T23:02:49+09:00,/tmp/test1,,IN_ISDIR,IN_CLOSE,IN_CLOSE_NOWRITE 2009-12-06T23:02:49+09:00,/tmp/test1,,IN_OPEN,IN_ISDIR 2009-12-06T23:02:49+09:00,/tmp/test1,,IN_ISDIR,IN_CLOSE,IN_CLOSE_NOWRITE 2009-12-06T23:02:49+09:00,/tmp/test1,,IN_OPEN,IN_ISDIR 2009-12-06T23:02:49+09:00,/tmp/test1,,IN_ISDIR,IN_CLOSE,IN_CLOSE_NOWRITE 2009-12-06T23:02:49+09:00,/tmp/test1,,IN_OPEN,IN_ISDIR 2009-12-06T23:02:49+09:00,/tmp/test1,,IN_ISDIR,IN_CLOSE,IN_CLOSE_NOWRITE 2009-12-06T23:02:49+09:00,/tmp/test1,,IN_OPEN,IN_ISDIR 2009-12-06T23:02:49+09:00,/tmp/test1,,IN_ISDIR,IN_CLOSE,IN_CLOSE_NOWRITE 2009-12-06T23:02:49+09:00,/tmp/test1,,IN_OPEN,IN_ISDIR 2009-12-06T23:02:49+09:00,/tmp/test1,,IN_ISDIR,IN_CLOSE,IN_CLOSE_NOWRITE 2009-12-06T23:02:49+09:00,/tmp/test1,,IN_OPEN,IN_ISDIR 2009-12-06T23:02:49+09:00,/tmp/test1,,IN_ISDIR,IN_CLOSE,IN_CLOSE_NOWRITE 2009-12-06T23:02:49+09:00,/tmp/test1,,IN_OPEN,IN_ISDIR 2009-12-06T23:02:49+09:00,/tmp/test1,,IN_ISDIR,IN_CLOSE,IN_CLOSE_NOWRITE 2009-12-06T23:02:49+09:00,/tmp/test1,,IN_OPEN,IN_ISDIR 2009-12-06T23:02:49+09:00,/tmp/test1,,IN_ISDIR,IN_CLOSE,IN_CLOSE_NOWRITE 2009-12-06T23:02:49+09:00,/tmp/test1,,IN_OPEN,IN_ISDIR 2009-12-06T23:02:49+09:00,/tmp/test1,,IN_ISDIR,IN_CLOSE,IN_CLOSE_NOWRITE 2009-12-06T23:02:49+09:00,/tmp/test1,,IN_OPEN,IN_ISDIR 2009-12-06T23:02:49+09:00,/tmp/test1,,IN_ISDIR,IN_CLOSE,IN_CLOSE_NOWRITE 2009-12-06T23:02:49+09:00,/tmp/test1,,IN_OPEN,IN_ISDIR 2009-12-06T23:02:49+09:00,/tmp/test1,,IN_ISDIR,IN_CLOSE,IN_CLOSE_NOWRITE 2009-12-06T23:02:49+09:00,/tmp/test1,,IN_OPEN,IN_ISDIR 2009-12-06T23:02:49+09:00,/tmp/test1,,IN_ISDIR,IN_CLOSE,IN_CLOSE_NOWRITE 2009-12-06T23:02:49+09:00,/tmp/test1,,IN_OPEN,IN_ISDIR 2009-12-06T23:02:49+09:00,/tmp/test1,,IN_ISDIR,IN_CLOSE,IN_CLOSE_NOWRITE 2009-12-06T23:02:49+09:00,/tmp/test1,,IN_OPEN,IN_ISDIR 2009-12-06T23:02:49+09:00,/tmp/test1,,IN_ISDIR,IN_CLOSE,IN_CLOSE_NOWRITE 2009-12-06T23:02:49+09:00,/tmp/test1,,IN_OPEN,IN_ISDIR 2009-12-06T23:02:49+09:00,/tmp/test1,,IN_ISDIR,IN_CLOSE,IN_CLOSE_NOWRITE 2009-12-06T23:02:49+09:00,/tmp/test1,.bb.swp,IN_CREATE 2009-12-06T23:02:49+09:00,/tmp/test1,.bb.swp,IN_OPEN 2009-12-06T23:02:49+09:00,/tmp/test1,.bb.swpx,IN_CREATE 2009-12-06T23:02:49+09:00,/tmp/test1,.bb.swpx,IN_OPEN 2009-12-06T23:02:49+09:00,/tmp/test1,.bb.swpx,IN_CLOSE_WRITE,IN_CLOSE 2009-12-06T23:02:49+09:00,/tmp/test1,.bb.swpx,IN_DELETE 2009-12-06T23:02:49+09:00,/tmp/test1,.bb.swp,IN_CLOSE_WRITE,IN_CLOSE 2009-12-06T23:02:49+09:00,/tmp/test1,.bb.swp,IN_DELETE 2009-12-06T23:02:49+09:00,/tmp/test1,.bb.swp,IN_CREATE 2009-12-06T23:02:49+09:00,/tmp/test1,.bb.swp,IN_OPEN 2009-12-06T23:02:49+09:00,/tmp/test1,.bb.swp,IN_MODIFY 2009-12-06T23:02:49+09:00,/tmp/test1,,IN_OPEN,IN_ISDIR 2009-12-06T23:02:49+09:00,/tmp/test1,,IN_ISDIR,IN_CLOSE,IN_CLOSE_NOWRITE 2009-12-06T23:02:51+09:00,/tmp/test1,bb,IN_CREATE 2009-12-06T23:02:51+09:00,/tmp/test1,bb,IN_OPEN 2009-12-06T23:02:51+09:00,/tmp/test1,bb,IN_CLOSE_WRITE,IN_CLOSE 2009-12-06T23:02:51+09:00,/tmp/test1,.bb.swp,IN_CLOSE_WRITE,IN_CLOSE 2009-12-06T23:02:51+09:00,/tmp/test1,.bb.swp,IN_DELETE 2009-12-06T23:03:00+09:00,/tmp/test1,cc,IN_MOVED_TO,IN_MOVE

    トラックバックについて
    Trackback URL:
    お気軽にどうぞ。トラックバック前にポリシーをお読みください。[policy]
    このエントリへのTrackbackにはこのURLが必要です→https://blog.cles.jp/item/3322
    Trackbacks
    このエントリにトラックバックはありません
    Comments
    愛のあるツッコミをお気軽にどうぞ。[policy]
    古いエントリについてはコメント制御しているため、即時に反映されないことがあります。
    コメントはありません
    Comments Form

    コメントは承認後の表示となります。
    OpenIDでログインすると、即時に公開されます。

    OpenID を使ってログインすることができます。

    Identity URL: Yahoo! JAPAN IDでログイン