How do I get my LaunchAgent to run as root? I want to run a process as root at user login.

I created /System/Library/LaunchAgents/eXist.plist with rx—r—r— permissions:

&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"&gt;
&lt;plist version="1.0"&gt;
    &lt;dict&gt;
        &lt;key&gt;Disabled&lt;/key&gt;
        &lt;false/&gt;
        &lt;key&gt;GroupName&lt;/key&gt;
        &lt;string&gt;wheel&lt;/string&gt;
        &lt;key&gt;Label&lt;/key&gt;
        &lt;string&gt;eXist DB&lt;/string&gt;
        &lt;key&gt;Program&lt;/key&gt;
        &lt;string&gt;/Applications/eXist-db/bin/startup.sh&lt;/string&gt;
        &lt;key&gt;RunAtLoad&lt;/key&gt;
        &lt;true/&gt;
        &lt;key&gt;StandardErrorPath&lt;/key&gt;
        &lt;string&gt;/tmp/eXist DB.err&lt;/string&gt;
        &lt;key&gt;StandardOutPath&lt;/key&gt;
        &lt;string&gt;/tmp/eXist DB.out&lt;/string&gt;
        &lt;key&gt;UserName&lt;/key&gt;
        &lt;string&gt;root&lt;/string&gt;
    &lt;/dict&gt;
&lt;/plist&gt;


However, my process continues to run as my user davea instead of root.  Any ideas what else I need to do?

I’m using Mac 10.9.1.
To get a user LaunchAgent executing a script to run as root you have to do the following: 

Modify the permissions of the script. Add the script (or command) to the sudoers file in the context of the user and finally add and load a proper launch agent plist.

This approach might create severe security holes!



In the example below I use the startup.sh of eXit-db 2.2:


Create a plist:

&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"&gt;
&lt;plist version="1.0"&gt;
&lt;dict&gt;
    &lt;key&gt;Disabled&lt;/key&gt;
    &lt;false/&gt;
    &lt;key&gt;Label&lt;/key&gt;
    &lt;string&gt;org.eXist_DB&lt;/string&gt;
    &lt;key&gt;ProgramArguments&lt;/key&gt;
    &lt;array&gt;
        &lt;string&gt;/usr/bin/sudo&lt;/string&gt;
        &lt;string&gt;/Applications/eXist-db.app/Contents/Resources/eXist-db/bin/startup.sh&lt;/string&gt;
    &lt;/array&gt;
    &lt;key&gt;RunAtLoad&lt;/key&gt;
    &lt;true/&gt;
    &lt;key&gt;StandardErrorPath&lt;/key&gt;
    &lt;string&gt;/tmp/eXist_DB.err&lt;/string&gt;
    &lt;key&gt;StandardOutPath&lt;/key&gt;
    &lt;string&gt;/tmp/eXist_DB.out&lt;/string&gt;
    &lt;key&gt;ThrottleInterval&lt;/key&gt;
    &lt;integer&gt;10&lt;/integer&gt;
&lt;/dict&gt;
&lt;/plist&gt;


and save it as org.eXist_DB.plist in ~/Library/LaunchAgents

The last key:

    &lt;key&gt;ThrottleInterval&lt;/key&gt;
    &lt;integer&gt;10&lt;/integer&gt;


might not be necessary. In my VM it was - for unknown reasons.
Modify the permissions of startup.sh:

sudo chown root /Applications/eXist-db.app/Contents/Resources/eXist-db/bin/startup.sh
sudo chmod 4755 /Applications/eXist-db.app/Contents/Resources/eXist-db/bin/startup.sh

Modify the sudoers file:

sudo visudo

add a line to enable executing a command without the need to enter the sudo  password

# User privilege specification
root    ALL=(ALL) ALL
%admin  ALL=(ALL) ALL


->

# User privilege specification
root    ALL=(ALL) ALL
%admin  ALL=(ALL) ALL
your_user_name ALL=(ALL) NOPASSWD: /Applications/eXist-db.app/Contents/Resources/eXist-db/bin/startup.sh

Finally enter:

launchctl load -w ~/Library/LaunchAgents/org.eXist_DB.plist


to load and start the launch agent