Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Branch: master

Ruby File Monitor

Ruby File Monitor is a easy way to watch the directories and files, execute commands when them changed. Use for situation like Auto refresh webpage, Auto execute unit tests, Auto compile CoffeeScript, Haml and so on. It's base on rb-inotify, So it only works in inotify supported system such as Linux.


Ruby >= 1.8.7

Linux Kernel >= 2.6.13


  1. Auto watching recursively:

    If make a new directory in the watched directory, the new directory will be watched automatically.

  2. Support Filter:

    Support filter directories or files.

  3. Events Buffer Mechanism:

    To avoid run the check methods too quickly, for example, when delete 20 files at the same time, if without Events Buffer will run the check methods 20 times. the frequency of file-monitor is 0.2 second

  4. High Performance

    For Ruby File Monitor use inotify library, so it's very fast, even watching thousands of directories.


Install from source

git clone
cd ruby-file-monitor
gem build file-monitor.gemspec
gem install --local --user-install file-monitor-0.1.3.gem 

Install from gem server

gem install file-monitor


#!/usr/bin/env ruby
# coding: utf-8
# File: examples/use-creator.rb

require 'rubygems'

lib_dir = File.join File.dirname(__FILE__), '../lib'
$:.unshift lib_dir unless $:.include? lib_dir

require 'file-monitor'

dir = ARGV[0] || '.'

# watch current working directory dir do

  # set frequency 0.2 second (optional default is 0.2)
  # frequency 0.2

  # do not follow the symlink (optional default is false)
  # follow_symlink false

  # do not watch directory contains git and svn
  # the last charactor '/' has been trimmed
  dirs {
    disallow /git$|svn$/

  # record ruby files only
  # it equals files /\.rb$/
  files {
    disallow /.*/
    allow /\.rb$/

  # The commands will be runned when file changed
  # the events contains all file modified infomation in last 0.2 second
  exec {|events|
    puts events.size()
    puts "do something"


Auto F5

Auto F5 will auto refresh the webpage in browser when any watched files changed. It's simple but very useful.

f5.rb requires sinatra.


1. The watched page MUST have </body> tag. 
   f5.rb will insert script  before </body> to refresh the page 
   when physical file changed.

2. Only support GET requests.


the website physical path is /var/www/foo
the host is
homepage is

start watching the directory
ruby examples/f5.rb /var/www/foo

open in browser
you will see the page same as
do some changes on /var/www/foo/index.html then save the file.
or create or modify any file in /var/www/foo will refresh automatically.


$ ruby examples/use-block.rb 
watching .
watching ./lib
ignore ./.git
watching ./examples

Edit and save by gvim, examples/use-block outputs

+  ./4913
-  ./4913
-  ./
+  ./
#  ./
5 events
do something
Something went wrong with that request. Please try again.