Getting launchd to read program arguments correctly I have a launchd script where the command I'm trying to run is erroring (apparently that's not a word, it is now), complaining of improper usage.

The specific error I'm getting is the command's usage text dumped to the system log. From this I infer the other info (path to the command, timing etc.) in the plist is being parsed correctly, just not the command's options.

After the command usage I have one last line:

18/11/2013 09:30:00.101 com.apple.launchd.peruser.501: (fake.lable.seti[33833]) Exited with code: 1


But that just means "I exited with an error".

I know launchd splits the command from its options and in the man page tells you about ProgramArguments: "...Please note: many people are confused by this key. Please read execvp(3) very carefully!.."

Well I read execvp(3) and I'm none the wiser, so I'm asking you lovely lot.

Normally, running the command from the terminal it'd look like this:

/Library/Application\ Support/BOINC\ Data/boinccmd --host localhost --passwd gobbledygook --project http://setiathome.berkeley.edu/ update


This works a treat.

And this is how I've split it in the Program/ProgramArguments section of my LaunchAgent plist:

&lt;key&gt;Program&lt;/key&gt;
    &lt;string&gt;/Library/Application Support/BOINC Data/boinccmd&lt;/string&gt;
&lt;key&gt;ProgramArguments&lt;/key&gt;
    &lt;array&gt;
        &lt;string&gt;--host localhost&lt;/string&gt;
        &lt;string&gt;--passwd gobbledygook&lt;/string&gt;
        &lt;string&gt;--project http://setiathome.berkeley.edu/ update&lt;/string&gt;
    &lt;/array&gt;


(for the record, I originally had the path to boinccmd \escaped out, but that doesn't work, launchd escapes spaces in the path for you)

I've tried splitting the arguments down further:

&lt;key&gt;Program&lt;/key&gt;
    &lt;string&gt;/Library/Application Support/BOINC Data/boinccmd&lt;/string&gt;
&lt;key&gt;ProgramArguments&lt;/key&gt;
    &lt;array&gt;
        &lt;string&gt;--host&lt;/string&gt;
        &lt;string&gt;localhost&lt;/string&gt;
        &lt;string&gt;--passwd&lt;/string&gt;
        &lt;string&gt;gobbledygook&lt;/string&gt;
        &lt;string&gt;--project&lt;/string&gt;
        &lt;string&gt;http://setiathome.berkeley.edu/&lt;/string&gt;
        &lt;string&gt;update&lt;/string&gt;
    &lt;/array&gt;


But that didn't seem to work either.

As ever, I'm very sure I'm missing something so simple.

Thanks.



ANSWER:
The first line of ProgramArguments needs to be the path to the program. This is what was tripping me up and indeed what was probably meant by the "...Please read very carefully!.." comment :)
I also found I had to split the arguments down to their component parts. When I had all that in place the whole thing works a charm. Thank you very much.

&lt;key&gt;Program&lt;/key&gt;
    &lt;string&gt;/Library/Application Support/BOINC Data/boinccmd&lt;/string&gt;
&lt;key&gt;ProgramArguments&lt;/key&gt;
    &lt;array&gt;
        &lt;string&gt;/Library/Application Support/BOINC Data/boinccmd&lt;/string&gt;
        &lt;string&gt;--host&lt;/string&gt;
        &lt;string&gt;localhost&lt;/string&gt;
        &lt;string&gt;--passwd&lt;/string&gt;
        &lt;string&gt;gobbledygook&lt;/string&gt;
        &lt;string&gt;--project&lt;/string&gt;
        &lt;string&gt;http://setiathome.berkeley.edu/&lt;/string&gt;
        &lt;string&gt;update&lt;/string&gt;
    &lt;/array&gt;


A final edit to say for an easy to understand explanation as to WHY this should be, see SirPavlova's explanation.

~W
The Program key specifies the file to execute, &amp; the ProgramArguments key specifies the arguments which will be passed to the executing process. Strictly speaking you can pass whatever arguments you want to a process, but the convention is that the first one should be the name by which the process was invoked, so most programs ignore their first argument.‡ The file to execute is obviously necessary information, but if the  Program key is is missing, launchd pretends it has the same value as the first argument in ProgramArguments purely as a convenience.

Your first example starts boinccmd &amp; gives it arguments that would be equivalent to the terminal command

--host\ localhost --passwd\ gobbledygook --project\ http://setiathome.berkeley.edu/\ update


which tells boinccmd that you invoked it as "--host localhost" &amp; only passed it two weird arguments.

Your second example separates the arguments correctly, but as Eddie Kelley suggested it needs one inserted at the front. It tells boinccmd that you invoked it as "--host", then passed another six arguments. boinccmd can recognise the last five as being two options, but has no idea what the "localhost" business is about. As far as boinccmd can tell, it was invoked from the terminal as

/Library/Application\ Support/BOINC\ Data/boinccmd localhost --passwd gobbledygook --project http://setiathome.berkeley.edu/ update


(note the missing "--host").

boinccmd is probably one of the great majority of programs that don't care what their first argument is, so you could probably actually just shove &lt;string&gt;HELLO&lt;/string&gt; at the head of the ProgramArguments array, but it's probably cleaner to remove the Program key altogether &amp; just use this:

&lt;key&gt;ProgramArguments&lt;/key&gt;
    &lt;array&gt;
        &lt;string&gt;/Library/Application Support/BOINC Data/boinccmd&lt;/string&gt;
        &lt;string&gt;--host&lt;/string&gt;
        &lt;string&gt;localhost&lt;/string&gt;
        &lt;string&gt;--passwd&lt;/string&gt;
        &lt;string&gt;gobbledygook&lt;/string&gt;
        &lt;string&gt;--project&lt;/string&gt;
        &lt;string&gt;http://setiathome.berkeley.edu/&lt;/string&gt;
        &lt;string&gt;update&lt;/string&gt;
    &lt;/array&gt;


‡ It can seem like a meaningless redundancy, but some programs use this to good effect: bash et al. act as login shells if their first argument begins with -, &amp; Vim enters various emulation modes if its first argument is ed or vi instead of vim.