Hacker News new | comments | show | ask | jobs | submit login

Another approach would be to use the Linux kernel's fantastic inotify() feature, which does not require running tools designed for other purposes...



There is also a less well utilized fsnotify feature for filesystem-wide notifications. Here is a cross-platform go implementation which also supports OSX: https://github.com/fsnotify/fsnotify

Hey! OP here. I submitted this exactly because I kept running into problems with inotify-tools, and other naive inotify wrappers, where tests would execute multiple times in quick succession, or stop executing on file changes, or other odd behavior.

Entr on the other hand works like a charm. You can see on the website that it details a number of edge cases with inotify that it works around. I'm not even sure it's an exhaustive list.

Note that that particular go implementation is prone to deadlocks.

Also, inotify is suboptimal for this use case. inotify has no recursion support, which means that the user must keep a watch on all files individually. There is also a limit to the number of inotify watches that can be in place at any one time.

A more modern, and more suitable interface for this use case is fanotify[0]. It doesn't provide all the functions that inotify does, but importantly it supports recursion, which alone would seem to make it a more obvious choice for this task.

0: http://man7.org/linux/man-pages/man7/fanotify.7.html

> Note that that particular go implementation is prone to deadlocks.

Any more information on this? I'm curious to read why.

inofity is no fantastic. Memory use scales with the number of directories watched. The program that uses the inotify system call has to keep a mapping of file descriptors to paths. Inotify is slow, inefficient and error-prone for recursively watching file systems.

fanotify is not an alternative because it cannot monitor file deletion or renames and it requires root permission.


> The program that uses the inotify system call has to keep a mapping of file descriptors to paths

If it doesn't care what file is modified and just wants to run a command on any change, why would it need to do this?

In that case it's not needed. One could imagine that one would like to start a specific command. That is what 'tup monitor' does.

Another use-case is a desktop search tool.


I myself quite enjoy using the very nice fswatch[0].


Indeed, I live by this one-liner which makes life so smooth:

    $ fswatch -0 -event ./main.lua | xargs -0 -n 1 -I {} lua ./main.lua

I made something similar with fsnotify recently: https://github.com/spelufo/on-change

The program discussed in the submission uses inotify

Err, not as far as I can see.

It has a header file that on Linux emulates kqueue using inotify: https://bitbucket.org/eradman/entr/src/1dc74ab543e2bced2994d...

Aha. Be that as it may, using inotify directly from the shell is still a fair approach which is worth putting out there.

Windows counterpart: FindFirstChangeNotification or SHChangeNotifyRegister().

Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | DMCA | Apply to YC | Contact