Help with bsexec and launchc

jmb03012
New Contributor III

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
4 REPLIES 4

mm2270
Legendary Contributor III

@jmb03012 Unfortunately, launchctl bsexec is very spotty on 10.9 in my experience. I've seen success rates when using it of around 50% in general, meaning it fails about half the time, which is pretty bad. I don't know if its just a bug in that OS or with the bootstrap command in general, but I once had to code my script to do a 5x loop over the command, iterating a counter each time it ran, and keep going until it either hit the 5x limit, or was successful in running the command as the user. Its a huge pain in the neck that it fails so often. And launchctl asuser only exists in 10.10 and up as we discussed on another thread, so its not an option to use it for 10.9 and earlier.
I wish I had a better answer than this for you, but so you know, you're not alone in having problems getting it to work reliably on Mavericks systems. I suspect even that this is part of why 'asuser' was added to launchctl in 10.10. I think even Apple knew it worked poorly in earlier OSes.

jmb03012
New Contributor III

@mm2270

Would it be possible for you to share the code for that loop and success check you mentioned?

mm2270
Legendary Contributor III

Sure, but I need to locate it again. I haven't needed to use it recently since we've been mostly on 10.10 now. I'll find it and post the code here later though.

jmb03012
New Contributor III

Great thank you so much, I just need to see how you are doing the loop because I cant figure out how to do a while type loop to check if the command was successful when I need to use bsexec to check if the agent actualy got loaded/unload.

Such a shame, all this work for an OS we only have around for a few specific reasons still.