Can't get launchd to run shell script as root

I suspect I'm missing something simple here, but I can't make this work, so maybe somebody else can tell me what's wrong.

I'm trying to run a very simple shell script on a regular basis to change permissions on files added to a group of folders on a server. I'm trying to set it up via launchd, since that's what Apple recommends, but when it runs, I get "operation not permitted" errors in the log, which I believe are caused by the script running as admin instead of root. I've tried adding the user variable "root" to the launchd plist, but when I load it that way, the log says it's ignored. Also, it doesn't load at boot. I can load it manually if I use launchctl load from the command line, but seems to run as admin instead of root.

Here's a copy of the launchd 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.mycompany.permissionsfix</string>
<key>ProgramArguments</key>
<array>
<string>sh</string>
<string>/usr/local/bin/permissionsfix</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>StartInterval</key>
<integer>60</integer>
</dict>
</plist>

It's named "com.mycompany.permissionsfix" and lives in /Library/LaunchDaemons. I believe the permissions are correct (System r/w, Wheel r, Others r). I've verified the script works when called from the command line with sudo.

Other variables that may or may not matter:
The folders being targeted live on a SAN volume shared by the server.
Server is a current model Xserve, running 10.6.2 server.

Can anybody tell me what I'm missing?

iMac, Mac OS X (10.6.2)

Posted on Apr 2, 2010 5:21 AM

Reply
4 replies

Apr 2, 2010 6:48 AM in response to foilpan

Here's the script:

#!/bin/bash
chmod -R 777 /Volumes/ODHome/Common/ADMS\ Documents\ to\ Case
exit #

Console log error (one for each file) when the script runs via launchd:

4/2/10 9:22:51 AM com.aimaudit.permissionsfix[6556] chmod: Unable to change file mode on /Volumes/ODHome/Common/ADMS Documents to Case/Prem Ck a Related Doc/227425.pdf: Operation not permitted
4/2/10 9:22:51 AM com.apple.launchd.peruser.501[178] (com.aimaudit.permissionsfix[6556]) Exited with exit code: 1

I can replicate the error if I run the script form the command line without using sudo, so I'm fairly certain launchd isn't running it as root. If I can make that happen, it ought to work.

Apr 2, 2010 8:17 AM in response to ClintR

did you load it with something like this?

sudo launchctl load -w /Library/LaunchDaemons/com.aimaudit.permissionsfix.plist


i doubt this is the problem, but i generally use the full path or set the PATH variable in the script to ensure calls to existing binaries work, so /bin/chmod instead of just chmod.

if you need to periodically change perms on the folder in question, perhaps there's a better way to ensure the permissions "stick," like using ACLs. is that an option?

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Can't get launchd to run shell script as root

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.