Track File Changes with Ruby & inotify

Discussion in 'Web Development' started by pradeep, Sep 20, 2014.

  1. pradeep

    pradeep Team Leader

    Apr 4, 2005
    Likes Received:
    Trophy Points:
    Kolkata, India
    Home Page:
    Ruby has become quite popular these days for writing maintenance & monitoring scripts, and a feature many of these kinds of scripts require is tracking files & directories for changes. Ruby has a few gems which let you accomplish this task easily, the one we are going to discuss about is rb-inotify.

    rb-inotify is a simple wrapper written using FFI around the inotify system calls, which is boon for people using shared servers or accounts with restricted access as there is no C/C++ extension to be compiled, so very easy to install. Those who have used the C API will feel very comfortable with it.

    Installing rb-notify gem

    Issue the following command from a shell:

    $ gem install rb-inotify

    Using rb-notify to track changes to files & directories

    rb-inotify uses the inotify system library, so essentially rb-notify can track all attributes that it supports, here's a list of masks & their being description:

    access          File was accessed (read) 
    attrib          Metadata changed (permissions, timestamps, extended attributes, etc.) 
    close_write     File opened for writing was closed 
    close_nowrite   File not opened for writing was closed 
    create          File/directory created in watched directory 
    delete          File/directory deleted from watched directory 
    delete_self     Watched file/directory was itself deleted
    modify          File was modified
    move_self       Watched file/directory was itself moved
    moved_from      File moved out of watched directory 
    moved_to        File moved into watched directory 
    open            File was opened
    Now, let's get our hands dirty with some code:

    require 'rubygems'
    require 'rb-inotify'
    notifier ="/tmp/pradeep", :create, :delete, :attrib) do |event|
    	event.flags.each do |flag|
    		## convert to string
    		flag = flag.to_s
    		puts case flag
    			when 'create' then "#{} created in /tmp/pradeep"
    			when 'delete' then "#{} deleted in /tmp/pradeep"
    			when 'attrib' then "#{}'s attributes changed in /tmp/pradeep"
    ## loop, wait for events from inotify
    [pradeep@glentauchers]$ ruby aa.rb &
    [1] 20060
    [pradeep@glentauchers]$ echo '1' > /tmp/pradeep/pop.op01
    [pradeep@glentauchers]$ pop.op01 created in /tmp/pradeep
    [pradeep@glentauchers]$ rm !$
    rm /tmp/pradeep/pop.op01
    pop.op01 deleted in /tmp/pradeep
    [pradeep@glentauchers]$ chmod +x /tmp/pradeep/pop.op00
    pop.op00's attributes changed in /tmp/pradeep
    It's as simple as that, try it out and you may find it a lot useful when required.
    shabbir likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice