1

I need to run a script in intervals that requires a root user to execute. I created a plist that will call a script in ~/bin/test.sh.

user@MBP:~$ ls -lah ~/bin/
-rw-r--r--   1 root       staff   566B Feb  6 23:44 test.plist
-rwxr-xr-x   1 root       staff    51B Feb  6 23:38 test.sh

Here are the contents:

user@MBP:~$ cat ~/bin/test.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.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.test.script</string>
    <key>ProgramArguments</key>
    <array>
        <string>sh</string>
        <string>/Users/user/bin/test.sh</string>
    </array>
    <key>StartInterval</key>
    <integer>30</integer>
</dict>
</plist>

user@MBP:~$ cat ~/bin/test.sh
#!/bin/sh
cp /Users/user/bin/dummy.txt /etc

Then I symlinked the plist in /Library/LaunchAgents.

user@MBP:~$ ls -lah /Library/LaunchAgents/
lrwxr-xr-x   1 root  wheel    31B Feb  5 06:28 com.test.script.plist -> /Users/user/bin/test.plist

But all of this doesn't work. When I look at /var/logs/system.log it just say:

Feb  7 14:42:12 MBP com.apple.launchd.peruser.501[242] (com.test.script`[925]): Exited with code: 1
2
  • 2
    Here's your solution :)
    – nuc
    Feb 7, 2013 at 10:21
  • Thank you! Found out about /Library/LaunchDaemons running as root too on my own just a couple of minutes ago. :)
    – mptp
    Feb 7, 2013 at 10:33

3 Answers 3

1

The man page for launchd.plist describes a UserName key that is required when running the job as root. Presumably, the value should be "root."

I'm no guru, but this seems to be the only thing you're missing — unless launch agents can't be symlinked.

0

So according to this SuperUser answer, you have to run launchctl as sudo in order to retain the permissions

sudo load -w /Library/LaunchDaemons/com.apple.samplelaunchdscript.plist
0

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

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

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .