
def watch_for(file, pattern)
f = File.open(file,"r")
f.seek(0,IO::SEEK_END)
while true do
select([f])
line = f.gets
puts "Found it! #{line}" if line=~pattern
end
end
watch_for("g.txt",/ERROR/)
Note: select always returns immediately when used on a file stream: You can always read EOF from that file stream, so your ruby process ends up spinning while it waits for the file to update. Different operating systems tend to offer different tools for waiting on files, Linux has inotify and OS X has fsevents - there are convenient ruby gems that wrap them, too.




近期评论