Getting UserName set in a launchd plist

Ryan Schmidt ryandesign at
Thu Sep 1 13:31:14 PDT 2011

On Sep 1, 2011, at 09:49, Cantor, Scott wrote:

> I found an old bug open on adding support for additional keywords in the
> startup plist created by the macport system, but it doesn't appear to have
> been addressed. The documentation simultaneously says that using
> start/stop instead of direct exec is less good, but that doesn't really
> fly if you're going to support a daemon running as a different user.
> A contributor to my project suggested a workaround, and I wanted to ask if
> it was kosher or not. He found the right macros to locate the plist and
> suggested:
> reinplace 
> "s|^<key>ProgramArguments</key>|<key>UserName</key><string>${runuser}</stri
> ng>\\
> <key>GroupName</key><string>${runuser}</string>\\&|g"
> ${prefix}/etc/${startupitem.location}/${startupitem.uniquename}/${startupit
> em.plist}
> Whether that's exactly correct or not, is it a bad idea to rely on those
> macros or to do that replacement?

I would say if you're going to be modifying a plist, you should be using a tool designed to do so, like "defaults" or "PlistBuddy". Not sure if we have any Tcl functions or wrappers around those utilities in MacPorts already, but having a wrapper around PlistBuddy especially would be nice of us to do, since PlistBuddy is sometimes hard to find. It's installed in /usr/libexec as of Snow Leopard, but before that, you have to go hunting through various OS X installer receipts to find a copy. Many Apple Installer packages use PlistBuddy, and have code that does that locating. Here's some other code that does that:

The rest of that article may also be relevant / helpful.

More information about the macports-users mailing list