How do I run a launchd command as root

What folder is the .plist stored in?

launchd runs Daemons (/Library/LaunchDaemons or /System/Library/LaunchDaemons) as root, and will run them regardless of whether users are logged in or not. Launch Agents (/Library/LaunchAgents/ or ~/Library/LaunchAgents/) are run when a user is logged in as that user. You can not use setuid to change the user running the script on daemons.

Because you will want to add it in /Library/LaunchDaemons you will want to make sure you load it into launchd with administrator privileges (eg. sudo launchctl load -w /Library/LaunchDaemons/com.apple.samplelaunchdscript.plist)

Check out man launchd for more information.


Have you tried using one of the launchd editors?

  • Lingon (abandoned, but supposedly works on Leopard and Snow Leopard)
  • Launchd Editor
  • launchctl

To make sure it is run as root, I'm pretty sure launchd will run the programs as root. Ever think of giving ownership of the script to root using chmod? This way, it won't run unless run as root. You need to then verify that it runs.

sudo chown root:admin script_to_run_by_launchd

Property lists in LaunchAgents also work, but you have to load both agents and daemons with sudo:

sudo chown root /Library/LaunchAgents/test.plist
sudo launchctl load /Library/LaunchAgents/test.plist

If the plist doesn't have a disabled key, it is loaded on the next login or restart by default, and -w is not necessary.

Technical Note TN2083: Daemons and Agents:

A daemon is a program that runs in the background as part of the overall system (that is, it is not tied to a particular user). A daemon cannot display any GUI; more specifically, it is not allowed to connect to the window server.

[...]

An agent is a process that runs in the background on behalf of a particular user. Agents are useful because they can do things that daemons can't, like reliably access the user's home directory or connect to the window server.