Using rb-inotify to monitor file changes

Example

require 'rb-inotify'


def new_notifier()
  notifier = INotify::Notifier.new

  notifier.watch("/home/james/notes.txt", :close_write) do |event|
    puts "File modified at " + Time.now.to_s
    notifier.stop
    new_notifier
  end
  notifier.run
end

new_notifier

In the above example the notifier is called from within a recursive method to ensure I am notified everytime the file is saved. I had tried to run a single notifier, however it wouldn't notify me when a file had been saved after the 3rd notice.

Resources

Tags:
Source:
1648hrs.txt
Published:
22-01-2015 16:48