How can I determine the invoking user in an Apple installer postinstall script
Solution 1:
After some testing, I believe I have found that one of the best options for this is
INSTALLER_USER=$(stat -f '%Su' $HOME)
I have found that the $HOME
environmental variable of the user installing the package is passed through, even after elevating for Installer.app
or using the installer
command.
The previous answers are assuming some things about the environment that may not always be true.
For example CONSOLE_USER=$(ps aux | grep console | grep -v grep | cut -d' ' -f1)
does not work in there are multiple users logged in, as may be the case in a computer lab environment.
As for ps aux | grep "CoreServices/Installer" | grep -v grep | awk '{print $1;}'
; this only works if the user is actually running Installer.app
and would not apply to someone running it with the installer
command in a shell.
Solution 2:
You can determine who owns the "console" and invoke sudo -u
command.
Something like this:
CONSOLE_USER=$(ps aux | grep console | grep -v grep | cut -d' ' -f1)
sudo -u $CONSOLE_USER COMMAND_TO_EXECUTE