3

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?


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.crashplan.status</string>
    <key>ProgramArguments</key>
    <array>
        <string>/Users/Rax/Library/Automation/Shell/crashplan_status</string>
    </array>
    <key>WatchPaths</key>
    <array>
        <string>/Library/Logs/CrashPlan/history.log.0</string>
    </array>
</dict>
</plist>
2

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.

<key>StandardErrorPath</key>
<string>/tmp/com.crashplan.status.stderr.log</string>
<key>StandardOutPath</key>
<string>/tmp/com.crashplan.status.stdout.log</string>

(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.

0

I know this is an old post, but as it has no real answer, I'll just post a suggestion. Do you still have the problem or has it been solved finally ? If not I may have a clue as I experimented the same problem.

In terminal, check if your watched file has extended arguments by using this command :

ls -l@

If your file has extended arguments like this :

com.apple.quarantine    32

try and type this command to remove extended arguments (use sudo if necessary) :

xattr -d -r com.apple.quarantine /Library/Logs/CrashPlan/history.log.0

in your case…

Looks like launchd watchpaths ignore files that have quarantine extended argument…

Hope it can help.

You must log in to answer this question.

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