How do I launch a process as a specific user at startup on OS X?

Posted by Scott Bonds on Server Fault See other posts from Server Fault or by Scott Bonds
Published on 2011-11-08T08:32:53Z Indexed on 2011/11/12 17:54 UTC
Read the original article Hit count: 217

Filed under:
|
|

I would like to run a script as a particular user on startup (not on login). I thought a launchd LaunchDaemon would do it, but 'man launchd' says:

"If you wish your service to run as a certain user, in that user's environment, making it a launchd agent is the ONLY supported means of accomplishing this on Mac OS X. In other words, it is not sufficient to perform a setuid(2) to become a user in the truest sense on Mac OS X."

They aren't kidding--when I try to run my script as a LaunchDaemon it doesn't work. In particular I'm trying to automate some keychain operations using the 'security' command, and it won't let me change the default keychain when I run the script through LaunchDaemon, though the script works fine when run using sudo from a shell.

A LaunchAgent won't work, because the goal is for the proces to run without a user logging in and LaunchAgents only run when someone logs in. I looked at cron and the @reboot directive and that looks promising, but I read that cron is deprecated on OSX.

© Server Fault or respective owner

Related posts about macosx

Related posts about startup