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

Advertisement

Leave a Comment so far
Leave a comment



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s



Follow

Get every new post delivered to your Inbox.