The problem is execution context and mach bootstrap in OS X (more info on Apple documentation worth reading). You need to simulate execution from proper context. In OS X it's done using launchctl bsexec
command. From TheDarkKnight's answer to Starting/stopping a launchd agent for all users with GUI sessions you have:
Unload Agents
#!/bin/bash
for id in `ps aux | grep -v grep | grep MyAgent | awk {'print $2'}`
do
launchctl bsexec $id launchctl unload /Library/LaunchAgents/myAgent.plist
done
Replace MyAgent
with the name of your Launch Agent.
Load Agents
#!/bin/bash
for pid_uid in $(ps -axo pid,uid,args | grep -i "[l]oginwindow.app" | awk '{print $1 "," $2}'); do
pid=$(echo $pid_uid | cut -d, -f1)
uid=$(echo $pid_uid | cut -d, -f2)
launchctl bsexec "$pid" chroot -u "$uid" / launchctl load /Library/LaunchAgents/myAgent.plist
done
On OS X 10.11 and Later
From the comment by vrrathod about OS X 10.11, El Capitain, use:
launchctl bootstrap gui/$uid /Library/LaunchAgents/myAgent.plist
You made some mistakes in your plist:
/Applications/eXist-db/bin/startup.sh probably doesn't exist if you have installed eXist-db 2.2
A valid path is /Applications/eXist-db.app/Contents/Resources/eXist-db/bin/startup.sh
StandardErorPath and StandardOutputPath are no valid keys
Valid keys are StandardErrorPath and StandardOutPath
probably the root <-> launchd problem already addressed by patrix
the plist doesn't have to be executable
To start the app after logging in with your user simply add it to System Preferences -> Users & Groups -> Your user -> Login Items
To start eXist-db 2.0 at boot time and jetty after logging in your user you have to do the following:
If you haven't done this already, first enter:
sudo /Applications/eXist-db/tools/wrapper/bin/exist.sh install
to install a LaunchDaemon org.tanukisoftware.wrapper.eXist-db.plist in /Library/LaunchDaemons/. If you want to add a StandardErrorPath and StandardOutPath modify the file with sudo nano /Library/LaunchDaemons/org.tanukisoftware.wrapper.eXist-db.plist
.
It should look like this finally:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Disabled</key>
<true/>
<key>Label</key>
<string>org.tanukisoftware.wrapper.eXist-db</string>
<key>ProgramArguments</key>
<array>
<string>/Applications/eXist-db/tools/wrapper/bin/exist.sh</string>
<string>launchdinternal</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>StandardErrorPath</key>
<string>/tmp/org.tanukisoftware.wrapper.eXist-db.stderr</string>
<key>StandardOutPath</key>
<string>/tmp/org.tanukisoftware.wrapper.eXist-db.stdout</string>
</dict>
</plist>
Load the daemon permanently with:
sudo launchctl load -w /Library/LaunchDaemons/org.tanukisoftware.wrapper.eXist-db.plist
Now create a second file in ~/Library/LaunchDaemons/ named com.eXist.plist with nano. It should look like this finally:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.eXist</string>
<key>Program</key>
<string>/Applications/eXist-db/bin/startup.sh</string>
<key>RunAtLoad</key>
<true/>
<key>StandardErrorPath</key>
<string>/tmp/com.eXist.stderr</string>
<key>StandardOutPath</key>
<string>/tmp/com.eXist.stdout</string>
</dict>
</plist>
A StandardErrorPath and StandardOutPath was added.
Load the agent permanently with:
launchctl load -w ~/Library/LaunchAgents/com.eXist.plist
Done.
Don't forget to set your (or the) JAVA_HOME variable properly. If you use a newer eXist-db release (e.g. 2.2) you have to add at least /Contents/Resources/ to the paths of exist.sh and startup.sh in the plist (please check the proper paths by opening the app bundle.
Hint: Don't use TextEdit to modify the plists: otherwise the plist files might be malformed.
Best Answer
I was using those commands for VirtualBox in rc.local, and I ran into the same problem. I don't if this is the right way to do this, but it works for me.
I created
/Library/LaunchDaemons/local.localhost.startup.plist
containing the code below. It runs the rc.local script once at start up.