Execute macOS scripts as Defined User
Description
By default, FileWave Client runs scripts and tasks as root on macOS. Use this pattern when a script needs to run a command as the currently logged-in console user.
Ingredients
- Text editor
- FileWave Central
- A test Mac managed by FileWave Client, with the intended user signed in to the desktop
Directions
sudo -u sets the target user. launchctl asuser runs the command in that user's launchd context. Some commands need one or the other; using both is a practical pattern for commands that expect the logged-in user session.
The script below finds the current console user and runs whoami in that user's context. It is a demonstration for an active desktop session: it does not wait for a user to sign in, check that the detected account is the intended user, or run for every local account. Do not use it at the login window as a per-user configuration workflow.
#!/bin/zsh
current_user=$(stat -f%Su /dev/console)
current_user_id=$(id -u "$current_user")
whoami
launchctl asuser "$current_user_id" sudo -u "$current_user" whoami
When FileWave runs the script with the intended desktop user signed in, the first whoami output shows the root user. The second output should show that desktop user. If the second command fails or reports a different account, resolve the session or account selection before replacing whoami with a command that changes user settings.
If the current logged-in user is sholden, the output should be:
root
sholden
For the Fileset setup and test sequence, see Fileset Scripts Overview. If the work needs to run at login or once for each user on a shared Mac, use the Outset login-script workflow instead of relying on whichever account owns the console when this script runs.
No comments to display
No comments to display