root is able to use
sudo to “become” any other user—this is a way to deploy a script as the active user.
Please note: The script running will need to know which user to
sudo to. So if you wanted to go this path, you'd want to run something like
CURRENTLY_LOGGEDON_USER=$(stat -f "%Su" /dev/console) so you can set the variable and then use it with
CURRENTLY_LOGGEDON_USER=$(stat -f "%Su" /dev/console)
sudo -u $CURRENTLY_LOGGEDON_USER
Please note: Each command must be prepended with
sudo -u $CURRENTLY_LOGGEDON_USER to be run as that user (not just once at the beginning of the script).
⚡️ IMPORTANT! ⚡️ The above is only an example. Please extensively test and debug a custom bash script before deploying it to your fleet.