So I have a LaunchAgent that is going to be deployed a systems running 10.9 through 10.11.
For unloading and loading the LaunchAgent as part of the pre/postinstall scripts in the installer and uninstaller packages, I am currently using the launchctl option asuser to handle this and its working great.
For the 10.9 machines, I was hoping to still be able to use the traditional sudo -u $CurrentUser type syntax but this fails saying that a db cant be read in var. Research further including a lot of posts on JAMF nation suggested using bsexec for the 10.9 and older systems.
I finally got the syntax figured out, but I get failures in varying rates, sometimes 1 out of every 10, other times in 1 out of every 2. I've tried adding additional s in case that was the issue, adding sudo at the beginning, changing the interpreter on the script from /bin/sh to /bin/bash but to no avail and I'm now completely stumped.
Here is one of the scripts that I am having an issue with, in this case the postinstall for the installer package that installs the LaunchAgent and then via this script would load it for the current logged in user. Just for clarification, only 10.9 and older are causing issues, 10.10 and 10.11 is fine.
When the bsexex command does fail, it yields the following error"
sudo: unable to execute /bin/bash: Bad address
launchctl bsexec failed: No such file or directory
#!/bin/sh
## postinstall
#Loads the LaunchAgent and starts BNotifier
###Variables###
OSVersion=$(sw_vers | grep ProductVersion)
Label=com.bloomberg.BNotifier
LoggedInUser=$(/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }')
LoggedInUID=$(id -u $LoggedInUser)
LoggedInPID=$(ps -axj | awk "/^$LoggedInUser/ && /Dock.app/ {print $2;exit}")
###Paths###
BNotifierLaunchAgent=/Library/LaunchAgents/com.bloomberg.BNotifier.plist
###Functions###
#This section intentionally left blank
###Script Contents - Do Not Modify Below This Line###
case $OSVersion in
*10.6*)
echo "Loading the BNotifier LaunchAgent"
/bin/launchctl bsexec "$LoggedInPID" /usr/bin/sudo -iu "$LoggedInUser" "/bin/launchctl load "$BNotifierLaunchAgent"" || echo "Error Loading "$BNotifierLaunchAgent""
echo "BNotifier LaunchAgent has been loaded"
;;
*10.7*)
echo "Loading the BNotifier LaunchAgent"
/bin/launchctl bsexec "$LoggedInPID" /usr/bin/sudo -iu "$LoggedInUser" "/bin/launchctl load "$BNotifierLaunchAgent"" || echo "Error Loading "$BNotifierLaunchAgent""
echo "BNotifier LaunchAgent has been loaded"
;;
*10.8*)
echo "Loading the BNotifier LaunchAgent"
/bin/launchctl bsexec "$LoggedInPID" /usr/bin/sudo -iu "$LoggedInUser" "/bin/launchctl load "$BNotifierLaunchAgent"" || echo "Error Loading "$BNotifierLaunchAgent""
echo "BNotifier LaunchAgent has been loaded"
;;
*10.9*)
echo "Loading the BNotifier LaunchAgent"
/bin/launchctl bsexec "$LoggedInPID" /usr/bin/sudo -iu "$LoggedInUser" "/bin/launchctl load "$BNotifierLaunchAgent"" || echo "Error Loading "$BNotifierLaunchAgent""
echo "BNotifier LaunchAgent has been loaded"
;;
*10.10*)
echo "Loading the BNotifier LaunchAgent"
/bin/launchctl asuser "$LoggedInUID" /usr/bin/sudo -iu "$LoggedInUser" "/bin/launchctl load "$BNotifierLaunchAgent"" || echo "Error Loading "$BNotifierLaunchAgent""
echo "BNotifier LaunchAgent has been loaded"
;;
*10.11*)
echo "Loading the BNotifier LaunchAgent"
/bin/launchctl asuser "$LoggedInUID" /usr/bin/sudo -iu "$LoggedInUser" "/bin/launchctl load "$BNotifierLaunchAgent"" || echo "Error Loading "$BNotifierLaunchAgent""
echo "BNotifier LaunchAgent has been loaded"
;;
esac