When my pkg runs its Post-installation script, how can I know how that happened?

299 Views Asked by At

There are two ways this script can be run: When the user opens the pkg file and goes through the normal GUI setup, or when an admin (or savvy user) runs sudo installer -pkg /path/to/Installer.pkg -target /. For the second one, I want to know when the script was run in this mode so I can make more admin-friendly decisions like not opening another GUI. How do I know when my pkg is installed via the command line?

I'm hoping for some environment variable or something similar.

2

There are 2 best solutions below

0
Ky - On BEST ANSWER

Since this was being run by the installer command, the COMMAND_LINE_INSTALL environment variable is set to 1. When opening the pkg normally, this variable is not set at all.

So:

if [ $COMMAND_LINE_INSTALL ]; then 
    # Do stuff for CLI land
else
    # Do stuff for GUI land
fi
1
Jeff Schaller On

Running the script via sudo will change the values of certain variables and add others in. Your script could check for the existence of those variables (or their values) to make a determination as to whether the installer was run via sudo.

Values that would get updated:

  • HOME
  • LOGNAME
  • MAIL

Values that would get set:

  • SUDO_COMMAND -- the command that was run via sudo
  • SUDO_GID -- the GID of the user that ran sudo
  • SUDO_UID -- the UID of the user that ran sudo
  • SUDO_USER -- the username of the user that ran sudo

My recommendation would be to check for the existence of the SUDO_COMMAND environment variable; it is unlikely to be set for a non-sudo installation, and would be set for a sudo-based installation.

Reference: sudo 1.8.20 manual - ENVIRONMENT section