Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm developing a web project using some libraries that force me to launch a shell command to compile some files everytime i modify them.

This task is obviously tiresome and i'm looking to find some script to handle it (I'm on OSX).

Is there a way to automatically launch a shell script when content of a given directory (+subdirs) is changed?

share|improve this question
This depends on your operating system. On Linux, use inotifywait (see e.g.…). – Gilles Aug 30 '10 at 7:03
up vote 5 down vote accepted

Mac OSX has two filesystem notification APIs: kqueue and FSEvents. I don't know about commands that give you access to them without programming. There is a python binding for FSEvents.

See also View Filesystem Access in Real Time on Mac OS X.

Given what you're trying to do, you might want to try a different approach: rather than trigger a recompile when the source files have been modified, trigger a recompile when the compiled files are requested (using make, so that nothing is actually recompiled if the source files haven't changed). This may be too costly if you have to do this for every request to a web site, though; benchmark to find out.

share|improve this answer
I used the python bindings for FSEvents and it worked (, thanks! – systempuntoout Aug 31 '10 at 12:55

The BSD command wait_on based on kqueie is available in MacPorts, it provides just what you need.

The wait_on command allows shell scripts to access the facilities provided by kqueue(3). This allows scripts to detect files being added to directories, data appended to files and many other things - all without polling.

In Linux I would use incron, INotify Cron.

share|improve this answer

Guard - is a cross-platform robust tool for watching directories (not just files) and running commands whenever files are added, modified or deleted.

It handles a mass of issues related to watching files, both OSX-specific and general file/directory watching problems.

It's also simple to setup and get running, and it uses the Listen library for watching files.

share|improve this answer

Not really, but you can get close. You can write a script that checks to see if the files in a directory have changed (probably by checking them against a file somewhere), and if so, build them.

Then, you can set cron to run this script every 10 minutes, so you know your stuff is no more than 10 minutes old.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .