Why is my 'launchd' task not responding to changes in watched files? I have a launchd task that is not triggering when a file changes, and can't figure out why it's failing.

When I load the .plist below with

launchctl load /Users/Rax/Library/LaunchAgents/com.crashplan.status.plist


the specified script is executed (once) and runs as expected (I can also run it successfully directly from the command line). But when the file on the watch path (/Library/Logs/CrashPlan/history.log.0) changes, nothing happens.

What could be missing that prevents the this task from responding to the file change?



&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"&gt;
&lt;plist version="1.0"&gt;
&lt;dict&gt;
    &lt;key&gt;Label&lt;/key&gt;
    &lt;string&gt;com.crashplan.status&lt;/string&gt;
    &lt;key&gt;ProgramArguments&lt;/key&gt;
    &lt;array&gt;
        &lt;string&gt;/Users/Rax/Library/Automation/Shell/crashplan_status&lt;/string&gt;
    &lt;/array&gt;
    &lt;key&gt;WatchPaths&lt;/key&gt;
    &lt;array&gt;
        &lt;string&gt;/Library/Logs/CrashPlan/history.log.0&lt;/string&gt;
    &lt;/array&gt;
&lt;/dict&gt;
&lt;/plist&gt;
FWIW I tried it here and it seems to be working for me. Any time I manually appended something to /Library/Logs/CrashPlan/history.log.0 the script did get triggered.

As such, this isn't really an answer, but it’s more a series of tips for debugging launchd:

A few tips for diagnosing launchd:

1) Use the stdout and stderr paths and see if anything gets logged to them. You can do that by adding these lines to your com.crashplan.status.plist file.

&lt;key&gt;StandardErrorPath&lt;/key&gt;
&lt;string&gt;/tmp/com.crashplan.status.stderr.log&lt;/string&gt;
&lt;key&gt;StandardOutPath&lt;/key&gt;
&lt;string&gt;/tmp/com.crashplan.status.stdout.log&lt;/string&gt;


(If multiple people use the same Mac, you might want to use a different path than /tmp/ but if it’s only you then it is as good of a place as any.)

2) With #1 you might also want to adjust your script (/Users/Rax/Library/Automation/Shell/crashplan_status) to include debugging info such as when it started and when it finished. That can be as simple as something like this added near the top of the script:

echo "$0: started at `date`"


and something like this near the end

echo "$0: finished at `date`"


3) With #2 you might also want to use something like terminal-notifier to show you when your script is being called, at least until you get past the debugging stage.

4) If none of that helps, you should check the exit status of whatever commands that you are calling in crashplan_status and see if those are exiting properly. For example, let's say that you were running echo in your crashplan_status 

5) Is your environment in launchd different from your shell in some way? This is best verified by adding this line near the top of your launchd script:

/usr/bin/printenv | /usr/bin/open -ef


which will cause the printenv to be sent to the stdout and the results opened in TextEdit.

The most common 'environment' problem that I run into is not having the $PATH set properly for launchd. It’s usually set in your shell init files such as .bashrc and inherited by any shell scripts you run from Terminal, but won't be for launchd.

You can see the path that launchd is using by:

launchctl getenv PATH


If you want to set it, you can set it by

launchctl setenv PATH


for example, for my system it would be:

launchctl setenv /Users/luomat/Dropbox/bin:/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin


If you don't want to have to remember to set that every time you start your computer, you can add it to /etc/launchd.conf by adding a line this:

setenv PATH /Users/luomat/Dropbox/bin:/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin


obviously change that to match your system. Also, don't be surprised if /etc/launchd.conf doesn't exist on your system. You may have to create it. To do that, I recommend a simple:

sudo pico -w /etc/launchd.conf


and when you are done editing, press control + X and follow the prompts to save the changes.