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
Best Answer
The man page for
launchd.plist
describes aUserName
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.