Triggering a build on file change in Linux

Posted in category shell on 2017-02-01
Tags: shell, bash, linux

inotifywait is a very nice tool that emits stream of events that happen to files in your project. That can help you setup a simple monitoring script that will trigger the build every time your change certain files. In my case it is F# files I am after:

Here at line #4 you can see my filter to only test for F# files. You can generalize it a bit to accept a parameter.

One observation that you might have is the type of events that inotifywait is asked to emit: moved_to and moved_from. Most of the modern text editors do not change files, but instead create a new temporary files and move it over the existing file. This might explain the problem that you do not see close_write events for the files you are interested in, but rather for some cryptic number based file names.

Let’s now try to generalize it a bit. First, it will need to accept number of extension names that should trigger build. Second, it will need to accept arbitrary command to evaluate as a build step. And last, it will need to accept the delay value (a number in seconds) to indicate how long should it wait before triggering supplied command. Here is how it might look now:

Notice that extensions should be passed to the script as comma-separated line, e.g. fs,cs,sh.

And now here is how you can call it for your project:

Happy hacking!