Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Oh how <code>launchd</code> drives me crazy.... </p> <p>To cut to the chase, after much study and experimentation, this is how I do it on 10.5+:</p> <pre><code># If possible, tell launchd to start the LaunchAgent. This will fail on 10.4. # $F[0] is the pid # $F[1] is the username # $F[2] is the first word of the command ps -ww -A -opid,user,command | \ perl -nae 'if($F[2] =~ /\bloginwindow\b/) { system( qq(launchctl bsexec $F[0] su $F[1] -c "launchctl load -w &lt;your_plist&gt;")) }' </code></pre> <p>I have found no way to achieve this directly on 10.4. I cheat on 10.4 and just run the thing the LaunchAgent would have run, even though it has a GUI and you're not supposed to be able to do that (you can anyway in 10.4-10.6; you can't in 10.7). On 10.4, the LaunchAgent works correct after the next reboot.</p> <p>The above code looks for <code>loginwindow</code> processes and uses <code>bsexec</code> to run commands in those contexts. Keep in mind that with Fast User Switching, there can be multiple contexts.</p> <p>Some useful links:</p> <ul> <li><a href="http://developer.apple.com/library/mac/#documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/Introduction.html" rel="nofollow noreferrer">Daemons and Services Programming Guide</a>. You have to read it, but it won't actually answer any of the hard questions. But it will at least give you hints at where everything is located.</li> <li><a href="http://developer.apple.com/library/mac/#technotes/tn2083/_index.html" rel="nofollow noreferrer">TN2083</a>. This is a maddening document that raises as many questions as it answers, but is gospel and mandatory reading for anyone entering the abyss of <code>launchd</code>.</li> <li><a href="https://stackoverflow.com/questions/1106638/starting-stopping-a-launchd-agent-for-all-users-with-gui-sessions">Starting/stopping a launchd agent for all users with GUI sessions</a>. This has several other useful links and explanation.</li> </ul> <p>IMO, <code>launchd</code> is one of the worst "great ideas" Apple has ever deployed. The idea is very useful, but the API is horrible.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload