Execute macOS scripts as Defined User
Description
By default, the FileWave Client executes scripts and tasks with elevated permissions (root on macOS). ThisThe additionbelow shows a method to the start oflaunch a script will execute itcommand as thean currently logged-inalternate user (alsowhen knownran asthrough the console user).FileWave.
Ingredients
- Text editor
- FileWave Central
Directions
Insertsudothiscommandnearmay be used to define a user to run a command. 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 in thestartsame command at the same time, increasing the chances ofyoursuccess.script,The
beforebelow method not only shows a method to define thedesireduser,actions.but#!/bin/zshgrabscurrent_user=$(statthe-f%Sucurrent/dev/console)'console'current_user_id=$(iduser.-u $current_user)Example:#!/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
echo "hello world" >> /Users/$current_user/Desktop/test.txt
The
When
If the current user logged in where 'sholden', the output should show:
root
sholden