Mac application installer needs to start user agent but not as root

Posted on 2008-11-11
Last Modified: 2012-05-05
I've built an install using Apple's PackageMaker utility.  It installs a daemon and a user agent.  At the end of the install I would like to start the user agent using launchctl.  However, since I had to elevate privileges during the install in order to install my daemon, I end up starting the user agent as root.  If I use sudo -u <user> launchctl, I just get an error: "launch_msg(): Socket is not connected".

Is there any way to un-elevate privileges during an install?


# this just errors out with launch_msg(): Socket is not connected".

# sudo -u current_user /bin/launchctl load ""/Library/LaunchAgents/com.myuseragent.plist"


#  this works but starts the user agent as root.  I want it to run as the current user

/bin/launchctl load "/Library/LaunchAgents/com.myuseragent.plist"

Open in new window

Question by:jimbojj
    LVL 9

    Expert Comment

    How are you determining the current user ?

    You have "current_user" but that's not a shell/bash internal so that's not going to work.

    From Nigel Kersten (see , afp548.comApple's Server mailing list, MacEnterprise list)

    to get the current logged in user
    /usr/bin/who | /usr/bin/grep console | /usr/bin/cut -d " " -f 1

    So you would use:

    current_user=$(/usr/bin/who | /usr/bin/grep console | /usr/bin/cut -d " " -f 1)

    You might also try specifying the user in your launchd plist via the provided
    UserName  key.


    You could also get the user in Python via
    import os

    Author Comment

    I came up with an applescript to solve my problem.  Since it will be run in the context of the logged-on user, my app starts correctly.

    So far I haven't seen any negative side affects from this.

    osascript -e "tell application \"System Events\"" -e "do shell script \"launchctl load -S Aqua /Library/LaunchAgents/com.myproduct.plist\"" -e "end tell"

    Open in new window

    LVL 9

    Accepted Solution

    Ok, but you hardly specified you wanted or needed an AppleScript-based solution, but yours is another way to go.
    What I replied with already does work.

    If you're running as root, you can run another command via:

    su <username> -c /path/to/binary

    sudo -u [username] [command]

    Author Comment

    I was looking for any solution that would work--I didn't have any specific technology in mind.

    Just launching the app won't solve the problem--when the current user logs out and another logs in, since nothing has told launchd to run it, it won't automatically start for the new user.

    As far as your previous suggestion, I need my launchd agent to run as whoever is logged in--which can be anybody.  I can't put a specific user in the .plist file.   Was this your solution?

    Author Closing Comment

    OK, you were right.  Just starting the app as the user works because when they log out launchd does refresh its list of what should be run and will start it anyway.  I like your solution better than mine because I HATE applescript. Thanks for your help!!  
    LVL 9

    Expert Comment

    Actually, what a good way to go would be to have your plist launch your script: that way, via the script,
    you can get the current user, and have the launch app as/for them.

    If you look at (don't modify of course !) some of the Apple plists in /System/Library/LaunchDaemons
    eg: cat /System/Library/LaunchDaemons/ssh.plist

    you can see that it does just that - invokes a script ( /usr/libexec/sshd-keygen-wrapper )that invokes the desired binary with specific options.

    LVL 9

    Expert Comment

    typo: "have the launch app" should read: have that launch the app

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    This article describes in detail how to set up the iPad in the Enterprise using iPCU aka iPhone Configuration Utility.  This could also be used for the iPhone although I have not detailed out any differences. Preparation as an iPad Administrator:…
    In this article we will discuss some EI Capitan Mail app issues and provide some manual process to resolve them.
    Users will learn how resize a batch of photos from a single command in Photoshop via Photoshop's Image Processor. Open up an Image you'd like to resize in Adobe Photoshop: Adjust the image size according to your preferences. Image > Adjustments > …
    Users will learn how to set proper sequence settings, scale images, paste attributes, add transitions, fades, and music. Open up Final Cut Pro 7 and Create a new Project: Set the Sequence Settings. a) Click File > Easy Setup > Format > Apple ProRe…

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now