guitsaru


Document Monitoring with Ruby
May 29, 2008, 3:13 pm
Filed under: Uncategorized

I have started a project at work to monitor log files and send any errors to the engineers. I played around with it in Ruby and in no time had the code below, which worked wonderfully. Unfortunately, due to the way this particular project is deployed, ruby was not an option. Feel free to use this if you need to monitor files for changes.

require 'ftools'

log_file = "test.log"
last_change = File.mtime(log_file)

while(true)
  if File.readable?(log_file) && (file_time = File.mtime(log_file)) > last_change
    last_change = file_time
    puts "Log has been updated"
  end
end

No Comments Yet so far
Leave a comment



Leave a comment
Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>