Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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
share|improve this question
2  
Here's your solution :) –  nuc Feb 7 '13 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 '13 at 10:33
add comment

3 Answers 3

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.

share|improve this answer
add comment

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
share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.