Ask Different is a question and answer site for power users of Apple hardware and software. 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

For example, I'd like to automatically run YUI Compressor whenever I edit a JavaScript source file to output a compressed version.

Ideally the process does not run in the background but in a dedicated shell, so I can easily terminate the process at the end of the day.

What would be the cleanest and most flexible way to watch specific files and/or directories for file changes and perform a custom action inside a shell?

share|improve this question

The $25 Codekit does watch folders and files for changes and does compress/minify/combine JavaScript and CSS files.

I know this is only a partial answer for your question, as it is not a generic watcher, but it does fit your example:

whenever I edit a JavaScript source file to output a compressed version

share|improve this answer
Thanks, it looks like a handy app, but it's not exactly the solution I was looking for. I'd really like something generic that can watch files and perform any custom command on changes. – Gerry Aug 7 '12 at 13:30

Watching a file for changes can be accomplished with a LaunchAgent. For example create a plist file at ~/Library/LaunchAgents/ and fill it with this content:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "">
<plist version="1.0">

Now make sure that the file /private/var/radmind/client/.radmindOnDemand does exist. Then load the launchagent with command $ launchctl load ~/Library/LaunchAgents/ As soon as the file ~/Library/LaunchAgents/ does no longer exist, this launchagent job will be unloaded.

Now write some shell script that watches your file for changes, like:


while [[ true ]]
    chsum2=`md5 /private/var/radmind/client/.radmindOnDemand`
    if [[ $chsum1 != $chsum2 ]] ; then           
        chsum1=`md5 /private/var/radmind/client/.radmindOnDemand`
    sleep 2

Note: this shell script needs some extra work to be done to make it efficient. Now it polls every 2 seconds (sleep 2). Better is to exit the script after the compile command has executed. That requires the storage of the output of the md5 hash command that can survive the exit and relauch of this shell script.

share|improve this answer
Thank you. I don't really fancy writing a launch agent everytime though, I'm looking for a solution flexible enough to add watchers and specify the file to watch and the command to run as an argument on the fly. – Gerry Aug 10 '12 at 8:05

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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