When files change, take action – inotify-hookable

Automatically compile your program when you save a file. Or load the new code to a web server.
Running a command on save can make development faster and easier. Inotify-hookable makes this simple. It can recursively watch directories, ignore specific files and run any command.

Short version for gurus:

$ inotify-hookable -rw flask/ -i 'flask/init.wsgi' -c 'touch flask/init.wsgi'

The rest of us can read on.


It’s available in package management

$ sudo apt-get update
$ sudo apt-get -y install inotify-hookable

Watch Directory

Start watching and run a command as needed:

$ inotify-hookable --watch-directories flask/ --recursive  --ignore-paths='flask/init.wsgi' --on-modify-command 'touch flask/init.wsgi'

This watches directory “flask/” recursively (i.e. including any subdirs and their subdirs).
We ignore “flask/init.wsgi”, because that’s the file we touch in the command. Watching the same file we modify on command would trigger another command… This prevents infinite loop.
When modifications are detected, it runs the command ‘touch flask/init.wsgi’.
In some other cases, the command could be compilation. In this case, I’m developing Python Flask web applications, and I have set Apache to reload the code whenever the init.wsgi is modified.

Special Case: Shared Folder on Virtualbox

When using a shared folder on Virtualbox and Vagrant, the watch must be established on the host OS side (the real, main OS). Inotify-hookable does not seem to work on files in a folder shared from host OS.
So the host OS runs inotify-hookable. The guest (Vagrant) OS Apache will still see that the init.wsgi was touched, and reload the code.

Posted in Uncategorized | Tagged , , , , , , | Comments Off on When files change, take action – inotify-hookable

Comments are closed.