4

I'm aware about inotifywait/inotifywatch from inotify-tools, about entr (http://entrproject.org/) and that dozen of shell scripts around, but they are not what I need.

What I need is something similar to guard (https://github.com/guard/guard).

They way it works: you create a file in a directory that specifies what to monitor and what to do when those files change.

Guard is ok, but it is 1) resource consumptive (and on large projects its slow), 2) requires whole Ruby + bunch of dependency gems, 3) requires plugin to run shell commands.

There's also tup (http://gittup.org/tup/) and it is freaking awesome except one extremely stupid limitation: it can't output to directories other than the one where Tupfile is. Its way is to create a Tupfile in each subdirectory and that doesn't work for me.

What I want in the end: I specify file patterns and directories (if no, then watch all) in some file, run some command and it monitors stuff matching given rules, whenever files change / add it executes given commands. Thats it.

Any suggestions?

0

Here's what I quickly thought:

#!/usr/bin/env bash
trap exit SIGQUIT SIGINT
while :; do
  watch -n2 -g ls -l /path/to/dir && do_something && do_something_else
done

That checks for every file change within that directory.

Little explanation: watch -g exits with status code 0 when it finds a change in the command's output. When it exits, it executes the next commands linked with conditional shell expressions (can be as many as you want). Then you just loop that forever until you hit Control-C (that's what the trap exit SIGQUIT SIGINT is for).

Of course you would like to tinker this script a bit, like -n2 for the time interval for the updates. The more you update it the more resources you spend.

For individual files I'd recommend using the ls -l --time-format=+%s /path/to/file /path/to/another_file command, as it checks the modification dates and even permission and ownership changes.

There surely is a better and more optimized method for doing this, but this approach works and definitely consumes less resources than Guard. Now, it all depends on scalability. If you're going to recursively list all files with all its attributes every millisecond, it surely consume more, but you definitely don't need that. Just set a bigger interval, say 2 seconds like above, maybe 10 or 30 if your project doesn't change that much and you don't mind waiting some time for the command to kick in. And in my opinion, this approach is quite nice and simple.

-1

Watchdog is Python library and shell utilities to monitor filesystem events.

1
  • Did you mind expanding upon how this fits into what OP is looking for?
    – 50-3
    Sep 6 '13 at 0:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.