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> * 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 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> 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. 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. 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. Thank you. -- Scott * If you contact me off list replace talklists@ with scott@ *
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.
Thanks for all the data, very helpful. So let me test case this, as I still have issues, on the most basic of a test. On Jun 1, 2009, at 11:58 AM, Damien Sorresso wrote:
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.
My bad, sorry, that was a hand typed example, and I would have usually put arg/array correctly.
* 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.
Ok, thank you, test case so far is: $cat ~/Library/Application\ Support/launchd-test/tester #!/bin/bash echo `date` >> /Users/me/Desktop/launchdtest.txt echo "this is working" >> /Users/me/Desktop/launchdtest.txt ./tester $cat ~/Desktop/launchdtest.txt Mon Jun 1 12:07:41 PDT 2009 this is working It works, run interactively at least. I make a launchd item for it. Stored in ~/Library/LaunchAgents/ -rw-r--r-- 1 me staff 479 Jun 1 12:11 com.me.test.foo.plist <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.$ <plist version="1.0"> <dict> <key>Label</key> <string>com.me.test.foo</string> <key>ProgramArguments</key> <array> <string>/bin/bash</string> <string>/Users/me/Library/Application\ Support/ launchd-test/tester</string> </array> <key>RunAtLoad</key> <true/> <key>StartInterval</key> <integer>10</integer> </dict> </plist> Now, tail the log file, nothing, look at the syslog, every 10 seconds: Jun 1 12:15:11 macbook com.me.test.foo[1010]: /bin/bash: /Users/me/ Library/Application\ Support/launchd-test/tester: No such file or directory Jun 1 12:15:11 macbook com.apple.launchd[123] (com.me.test.foo[1010]): Exited with exit code: 127 I will look into the "WorkingDirectory" now, but I am stumped as to why this should not work as is. Thanks again for the pointers. -- Scott * If you contact me off list replace talklists@ with scott@ *
On Jun 1, 2009, at 12:17 PM, Scott Haneda wrote:
Thanks for all the data, very helpful. So let me test case this, as I still have issues, on the most basic of a test.
On Jun 1, 2009, at 11:58 AM, Damien Sorresso wrote:
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.
My bad, sorry, that was a hand typed example, and I would have usually put arg/array correctly.
* 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.
Ok, thank you, test case so far is: $cat ~/Library/Application\ Support/launchd-test/tester #!/bin/bash echo `date` >> /Users/me/Desktop/launchdtest.txt echo "this is working" >> /Users/me/Desktop/launchdtest.txt
./tester $cat ~/Desktop/launchdtest.txt Mon Jun 1 12:07:41 PDT 2009 this is working
It works, run interactively at least. I make a launchd item for it. Stored in ~/Library/LaunchAgents/ -rw-r--r-- 1 me staff 479 Jun 1 12:11 com.me.test.foo.plist
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs /PropertyList-1.0.$ <plist version="1.0"> <dict> <key>Label</key> <string>com.me.test.foo</string> <key>ProgramArguments</key> <array> <string>/bin/bash</string> <string>/Users/me/Library/Application\ Support/ launchd-test/tester</string> </array> <key>RunAtLoad</key> <true/> <key>StartInterval</key> <integer>10</integer> </dict> </plist>
Now, tail the log file, nothing, look at the syslog, every 10 seconds: Jun 1 12:15:11 macbook com.me.test.foo[1010]: /bin/bash: /Users/me/ Library/Application\ Support/launchd-test/tester: No such file or directory Jun 1 12:15:11 macbook com.apple.launchd[123] (com.me.test.foo [1010]): Exited with exit code: 127
I will look into the "WorkingDirectory" now, but I am stumped as to why this should not work as is. Thanks again for the pointers.
You're still escaping the space in "Application Support" unnecessarily. launchd is not a shell interpreter, and the contents of each element of ProgramArgument is passed directly to posix_spawn(2), so you don't need to escape anything. Use this instead:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs /PropertyList-1.0.$ <plist version="1.0"> <dict> <key>Label</key> <string>com.me.test.foo</string> <key>ProgramArguments</key> <array> <string>/bin/bash</string> <string>/Users/me/Library/Application Support/launchd- test/tester</string> </array> <key>RunAtLoad</key> <true/> <key>StartInterval</key> <integer>10</integer> </dict> </plist> -- Damien Sorresso BSD Engineering Apple Inc.
On Jun 1, 2009, at 12:22 PM, Damien Sorresso wrote:
Now, tail the log file, nothing, look at the syslog, every 10 seconds: Jun 1 12:15:11 macbook com.me.test.foo[1010]: /bin/bash: /Users/me/ Library/Application\ Support/launchd-test/tester: No such file or directory Jun 1 12:15:11 macbook com.apple.launchd[123] (com.me.test.foo[1010]): Exited with exit code: 127
I will look into the "WorkingDirectory" now, but I am stumped as to why this should not work as is. Thanks again for the pointers.
You're still escaping the space in "Application Support" unnecessarily. launchd is not a shell interpreter, and the contents of each element of ProgramArgument is passed directly to posix_spawn(2), so you don't need to escape anything. Use this instead:
Thank you, that did indeed solve it. -- Scott * If you contact me off list replace talklists@ with scott@ *
participants (2)
-
Damien Sorresso
-
Scott Haneda