views:

81

answers:

4

I'd like to use Bash to run a test suite automatically when I save any file in a given directory.

Is there a mechanism for bash to execute a given script on save events?

Thanks.

::EDIT::

I should have mentioned that I'm on using OSX.

A: 

You can use incron to detect when a file has been closed.

Ignacio Vazquez-Abrams
A: 
loentar
Dennis Williamson
dnotify looks perfect, except I'm on a Mac. Sorry, I should have mentioned that in my post.
Thomas
I should second what Dennis pointed to: don't use dnotify, use inotify instead.
rbp
+1  A: 

Edited: you (the OP) mentioned you use OSX. I'm not aware of any similar tools on OSX. There is a low-level system call (inherited from BSD) called "kqueue", but you'd have to implement your own user-level tool. There is a sample application from Apple, called "Watcher", but it's proof of concept only, and doesn't do what you want.

There is another thread about this on Stack Overflow (also inconclusive).

For lack of an appropriate tool, if you're using a specific programming language, I'd advise you to look for solutions already written for it. Otherwise, I think you're stuck to polling and managing the changes yourself...

Here's my original, Linux-based answer, for archival purposes:

If you're using Linux, you might want to take a look at inotify . More specifically, you can install inotify-tools, which include inotifywait.

With it, you can monitor files and directories for a number of events, such as access, modification, opening, closing and many others. inotifywait can exit once the specified event has been detected, and so a simple loop would get you what you want:

while :; do
    inotifywait -e modify /some/directory
    run_test_suite
done

By the way, many programming languages and environments already have their own continuous test runners (for instance, with Python you could use tdaemon, among others).

rbp
A: 

You can use inotify as explained here Inotify Example: Introduction to Inotify with a C Program Example

thegeek