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
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.
#!/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, the first whoami output shows the root user. The second output shows the active console user.
If the current logged-in user is sholden, the output should be:
root
sholden
No comments to display
No comments to display