[launchd-dev] General questions on launchd

Damien Sorresso dsorresso at apple.com
Mon Jun 1 11:58:02 PDT 2009


On Jun 1, 2009, at 11:29 AM, Scott Haneda wrote:
> Hello, first post, I have done my best to hit up nabble archives and  
> see if there are any answers, so far, no luck.  I am on 10.5.
>
> Where does one draw the line from stuffing a command into a plist  
> versus having a simple command call a more complicated script, even  
> if the script to be called is not that complicated?
>
> Example:
> <dict>
> 	<key>ProgramArguments</key>
> 	<array>
> 		<string>/bin/bash /Users/me/Library/Application\ Support/dir/ 
> script</string>
> 	</array>
> </dict>
> </plist>

The structure of ProgramArguments dictates that you specify one  
argument per array element. While what you have works, it's only  
because the shell you're targeting understands it. In other words,  
don't get in the habit. It's a one-time cost to structure your plist  
appropriately, and Property List Editor makes it easy.

> * How important is it to use full paths to binaries like `echo` and  
> `ls`, as long as I am sure that I have not mucked with the  
> environment variables to them?  Are environment variables cleared in  
> launchd, or do user launchd plists need to have care taken to make  
> sure an alias bash alias will get in the way?
>
> I have never been able to get that to work.  syslog tells me
> Jun  1 11:02:47 host com.apple.launchd[155] (com.me.foo[19809]):  
> posix_spawnp("/Users/me/Library/Application Support/dir/ 
> script", ...): No such file or directory
> Jun  1 11:02:47 host com.apple.launchd[155] (com.me.ALARM[19809]):  
> Exited with exit code: 1

launchd uses posix_spawn(2), which will search _PATH_DEFPATH, which is  
specified as "/usr/bin:/bin". Personally, I try to always use the full  
path to an executable. It's just good practice.

> I try to work around it with things like this:
> <dict>
> 	<key>ProgramArguments</key>
> 	<array>
> 		<string>cd</string>
> 		<string>/Users/me/Library/Application\ Support/dir/;</string>
> 		<string>./script</string>
> 	</array>
> </dict>

You should use the WorkingDirectory key instead. Please see  
launchd.plist(5).

> Those generally fail as well.  It seems to be spaces in the path  
> name, but there has to be a way to make spaces work. Application  
> Support is a perfect place for me to store my scripts, and logs from  
> those scripts, so far, no luck.  Pipes are also an issue, or  
> redirection, which I have not been able to get around.  A basic echo  
> "test" >> log.txt or one with a real | in it also do not work for me.

You don't need to escape spaces when specifying a property list  
string, as it is not a shell.

> A lot can be done in a short one liner as well, what is the trick,  
> what are the limits?
>
> I generally just stuff all my code into a script, ~/bin is usually  
> where I put it, and have launchd call it as /bin/bash /Users/me/ 
> scriptname
>
> I seem to have a lot of trouble unloading items.  There are cases  
> where I have tested, and then deleted a plist, forgot to unload it.   
> If I try to unload it, I of course have no file to point to, so I  
> try to unload it by name.  Neither works.

You should use `launchctl remove`. Please see launchctl(1).

> I have ended up rebooting, but consider sending a HUP to launchd,  
> but ps shows there to be a few more items in the list than I feel  
> safe just restarting.
>
> Can someone also point me to docs or explain to me the  
> 'WorkingDirectory' item.  I al always cd'ing to a location where I  
> do my work.  I do not mind continuing to do that, but if defining  
> 'WorkingDirectory' in a plist does what I think it does, it would be  
> nice to have, as only a trigger to the person debugging it as to  
> where the work is happening, without having to trace a ton of  
> scripts calling each other.


Please read the supplied documentation:

launchd(8)
launchctl(1)
launchd.plist(5)

There are some gaps in the Leopard man pages that we've cleaned up in  
SnowLeopard, but it sounds like the existing documentation should help  
you with all of the issues you've raised.
-- 
Damien Sorresso
BSD Engineering
Apple Inc.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2425 bytes
Desc: not available
URL: <http://lists.macosforge.org/pipermail/launchd-dev/attachments/20090601/4125d26b/attachment.bin>


More information about the launchd-dev mailing list