- blogs:
- cles::blog
2009/12/06
[ by hsur at 23:48]
Rubyからinotifyを使ってファイルやディレクトリを監視する
昨日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
# 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
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
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が必要です→http://blog.cles.jp/item/3322
Trackbacks
このエントリにトラックバックはありません
Comments
愛のあるツッコミをお気軽にどうぞ。[policy]
古いエントリについてはコメント制御しているため、即時に反映されないことがあります。
古いエントリについてはコメント制御しているため、即時に反映されないことがあります。
コメントはありません
Comments Form
コメントは承認後の表示となります。
OpenIDでログインすると、即時に公開されます。
OpenID を使ってログインすることができます。
Copyright © 2004-2010 by CLES All Rights Reserved.
sp-20100320123008644595555@cles.net
sp-20100320123008644595555@cles.net
サイト内検索
おしらせ
検索ワード
Tags [All Tags]
google | php | ruby | windows | firefox | linux | SoftwareEngineering | java | spam | ranking | curry | camera | seo | FireEmblem | thunderbird | server | marketing | mobile | Doctoral | docomo
へぇが多いエントリ [Top 100]
- おめでとうございます (4)
- 知恵の輪 サターン編 (3)
- SourceForge.JPのSubversion... (3)
- 人生初の出来事 (3)
- サーバセットアップ (3)
- ブックマークボタンを1つに (3)
- 和食 小錦 (3)
- 散髪しました (3)
- .inはインドのccTLDなのか (3)
- やっと髪をきりました (3)
閲覧数が多いエントリ [Top 100]
1 . ドラゴンクエストVIの影響力 [8989x]
2 . やっぱりあった!パクれる読書感想文! [7654x]
3 . Echofon for Firefox [6417x]
4 . 急性胃腸炎 [5781x]
5 . OpenIDで自分のサイトのURLを使う [5776x]
2 . やっぱりあった!パクれる読書感想文! [7654x]
3 . Echofon for Firefox [6417x]
4 . 急性胃腸炎 [5781x]
5 . OpenIDで自分のサイトのURLを使う [5776x]
最新のエントリ [archives]
最新のコメント [Latest 100]
- CD-ROM起動で、HDDを完全消去
- NP_Moblog v1.16
- pinzoro 01/15
- hsur 12/29
- and more...
- 耳がおかしいと思ったら突発..
- baca 01/13
- hsur 01/13
- and more...
カテゴリ別エントリ
cles::blogについて
Syndicate
Calendar
Referrers
Blog People
Admin
★はてな認証APIをつかってログインすることができます。
Powered by NP_Paint




