Skip to main content

Execute macOS scripts as Defined User

Description

By default, the FileWave Client executesruns scripts and tasks withas elevated permissions (root on macOS).macOS. TheUse belowthis showspattern when a methodscript needs to launchrun a command as anthe alternatecurrently userlogged-in whenconsole ran through FileWave.user.

Ingredients

  • Text editor
  • FileWave Central

Directions

Thesudo sudo-u sets the target user. launchctl asuser runs the command mayin bethat useduser's tolaunchd definecontext. Some commands need one or the other; using both is a userpractical topattern runfor acommands command.that Launchctl may also be used to define a user.  In some instances only one of these options may be successful.  However, both may be defined inexpect the samelogged-in commanduser at the same time, increasing the chances of success.session.

The script below method not only shows a method to define the user, but grabsfinds the current 'console'console user.user and runs whoami in that user's context.

#!/bin/zsh
current_user=$(stat -f%Su /dev/console)
current_user_id=$(id -u "$current_user)current_user")

whoami

launchctl asuser "$current_user_idcurrent_user_id" sudo -u "$current_usercurrent_user" whoami

When ranFileWave through FileWave,runs the output ofscript, the abovefirst will show thewhoami output of the 'whoami' command has altered, by first echoingshows the root usernameuser. andThe thensecond theoutput name ofshows the active console user of the device.user.

If the current logged-in user loggedis in where 'sholden'sholden, the output should show:be:

root
sholden