6

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?

1

5 Answers 5

5

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.

1
1

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.

1

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.

0

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.

0

As of 2023, I'd recommend looking at fswatch or guard, which both work on many platforms.

1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Feb 7, 2023 at 22:02

You must log in to answer this question.

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