How can I add arguments to OSX launchd plist to pipe output to a log file? How can I add arguments to OSX launchd plist to pipe output to a log file? bash bash

How can I add arguments to OSX launchd plist to pipe output to a log file?


>> is a shell operator, not a program argument, and launchd doesn't pass its commands through a shell (unless you force it, as in @Lauri's answer). But launchd does have its own option for redirecting stdout:

<key>StandardOutPath</key><string>/Users/ilium007/support/scripts/handbrake/logs/handbrake_encode.log</string>

Note that it automatically appends (equivalent to >>, not >). You can also redirect stderr with the StandardErrorPath key. One thing you cannot do is use ~ (as in ~/support/scripts/handbrake/logs/handbrake_encode.log) -- that, again, is a shell feature that launchd doesn't emulate.


The standard out and error keys are very useful, I redirected mine to the first terminal/shell as follows:

<key>StandardOutPath</key><string>/dev/ttys000</string><key>StandardErrorPath</key><string>/dev/ttys000</string>

You can change ttys000 to another number (ttys001..2) if you have multiple terminals. You can find the terminal id by typing in the shell:

tty

This helped me to find out why I was getting console errors in OS X, I needed to have the full path for every command and full paths for directories I was CD'ing to in scripts.

for instance grep is:

/usr/bin/grep

and commands installed by macports use other paths also, like:

/opt/local/bin/lynx

I also tried to chown root:wheel the plist, but this doesn't seem to be necessary, but probably not a bad idea to prevent someone from editing it to whatever...

edit:changed mine back to normal user after I got some permission errors which turned out to be because the script was trying to make files in / without permission, needed to add full paths to output files too, which is weird as it worked the previous day.


The redirection operators can't be used as arguments. You can either make another script for the pipeline or use bash -c.

<string>bash</string><string>-c</string><string>bash ~/Desktop/test.sh >> ~/Desktop/test</string>