I'm using the Packages app to create an installer which includes, among other things, a launchd job.
The postinstall Bash script needs to target the logged in user as follows:
launchctl bootstrap gui/$UID ~/Library/LaunchAgents/com.mycompany.myproduct.plist
The problem is that $UID returns 0 when run from within the package.
The following tweak works as expected on my machine but the target deployment users don't use 501 but mobile accounts so the UID differs with each AD account
launchctl bootstrap gui/501 ~/Library/LaunchAgents/com.mycompany.myproduct.plist
I've tried all sorts of things:
$SUDO_USER
$LOGNAME
$(who -m | awk '{print $1;})
$(logname 2>/dev/null || $SUDO_NAME)
All return the 0/root user.
This seems to work, on my machine, when run it Terminal after sudo su. But, as I don't understand the /dev/console bit (or the %Su parameter), is it 100% reliable?
id -P $(stat -f%Su /dev/console) | cut -d : -f 3