Skip to content

Instantly share code, notes, and snippets.

@opragel
Last active April 5, 2016 18:48
Show Gist options
  • Select an option

  • Save opragel/1a45a0a7193ceca3399a to your computer and use it in GitHub Desktop.

Select an option

Save opragel/1a45a0a7193ceca3399a to your computer and use it in GitHub Desktop.

Revisions

  1. Owen Pragel revised this gist Mar 25, 2016. 2 changed files with 29 additions and 20 deletions.
    20 changes: 0 additions & 20 deletions runLaunchAgentAsUser
    Original file line number Diff line number Diff line change
    @@ -1,20 +0,0 @@
    #!/bin/sh


    # variable-ing
    loggedInUser=$(/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }')
    loggedInUID=$(stat -f%u /dev/console)
    loggedInPID=$(ps -A | grep -m1 Dock | awk '{print $1}')
    osvers=$(sw_vers -productVersion | awk -F. '{print $2}')


    # modify LaunchAgent for correct ownership
    chown -R $loggedInUser "path/to/LaunchAgent"


    # load LaunchAgent as user (using launchctl bsexec or launchctl bootstrap depending on OS X version)
    if [[ $osvers -lt 10 ]]; then
    launchctl bsexec $loggedInPID /usr/bin/sudo -iu $loggedInUser "launchctl load 'path/to/launchagent'"
    else
    launchctl bootstrap gui/$loggedInUID "path/to/launchagent"
    fi
    29 changes: 29 additions & 0 deletions run_launchagent_as_user.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    #!/bin/bash

    LAUNCH_AGENT_PATH="/Library/LaunchAgents/com.pritunl.client.plist"

    currentUser=$(python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "\n");')
    currentUserPID=$(pgrep WindowServer -m1)
    currentUserUID=$(id -u "$currentUser")
    osvers=$(sw_vers -productVersion | awk -F. '{print $2}')

    if [[ $EUID -ne 0 ]]; then
    printf "Run this script as root."
    exit 10
    fi

    if [ ! -f "$LAUNCH_AGENT_PATH" ]; then
    printf "Launch agent not found at path: %s" "$LAUNCH_AGENT_PATH"
    exit 20
    fi

    if [[ $osvers -lt 10 ]]; then
    launchctl bsexec "$currentUserPID" launchctl load "$LAUNCH_AGENT_PATH" > /dev/null 2>&1 &
    elif [[ $osvers -eq 10 ]]; then
    launchctl asuser "$currentUser" launchctl load "$LAUNCH_AGENT_PATH" > /dev/null 2>&1 &
    elif [[ $osvers -ge 11 ]]; then
    launchctl bootstrap "gui/$currentUserUID" "$LAUNCH_AGENT_PATH" > /dev/null 2>&1 &
    else
    printf "Things have gone terribly wrong. OS Version: %s" "$osvers"
    exit 30
    fi
  2. @mpanighetti mpanighetti created this gist Mar 25, 2016.
    20 changes: 20 additions & 0 deletions runLaunchAgentAsUser
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    #!/bin/sh


    # variable-ing
    loggedInUser=$(/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }')
    loggedInUID=$(stat -f%u /dev/console)
    loggedInPID=$(ps -A | grep -m1 Dock | awk '{print $1}')
    osvers=$(sw_vers -productVersion | awk -F. '{print $2}')


    # modify LaunchAgent for correct ownership
    chown -R $loggedInUser "path/to/LaunchAgent"


    # load LaunchAgent as user (using launchctl bsexec or launchctl bootstrap depending on OS X version)
    if [[ $osvers -lt 10 ]]; then
    launchctl bsexec $loggedInPID /usr/bin/sudo -iu $loggedInUser "launchctl load 'path/to/launchagent'"
    else
    launchctl bootstrap gui/$loggedInUID "path/to/launchagent"
    fi