Posted on 09-18-2024 10:21 AM
This has been talked about a lot here and I see a lot of these posts, but man I cannot figure out what I am doing wrong. I am trying to write a script that will update a plist in the user folder as well as license the application upon installation. So I want the script to run post pkg install. I have this script and I am able to get it to run successfully locally, but clearly when jamf runs, it runs as root.
I have actually tried several ways to get it to run as the user, without success. The most recent option I tried was from here: https://community.jamf.com/t5/jamf-pro/need-help-forcing-script-to-run-commands-under-current-logged...
#!/bin/bash
#Get username
#Open OffShoot to make sure nl.syncfactory.Hedge.Mac.plist exists
myuser="$(id -u -n)"
echo "Username: $myuser"
sleep 1
echo "OffShoot Will Open To Create nl.syncfactory.Hedge.Mac.plist"
open /Applications/OffShoot.app
sleep 2
#Kill OffShoot
ps -ef | grep OffShoot | grep -v grep | awk '{print $2}' | xargs kill
sleep .5
#Set OffShoot Scripts and Open OffShoot
echo " Setting SFIDefaultsFirstAppStart to - False"
defaults write /Users/"$myuser"/Library/Preferences/nl.syncfactory.Hedge.Mac.plist SFIDefaultsFirstAppStart -string "False"
sleep .5
echo " Setting SFIDefaultsUserAcceptsResponsibilityForScripts - True"
defaults write /Users/"$myuser"/Library/Preferences/nl.syncfactory.Hedge.Mac.plist SFIDefaultsUserAcceptsResponsibilityForScripts "True"
sleep .5
echo " Setting SFIDefaultsHedgeStartedScript to callFilmsAPIOnOpen.scpt"
defaults write /Users/"$myuser"/Library/Preferences/nl.syncfactory.Hedge.Mac.plist SFIDefaultsHedgeStartedScript "/Library/OffShootConfig/AppleScripts/callFilmsAPIOnOpen.scpt"
sleep .5
echo " Setting SFIDefaultsDiskBusyScript to clear_dest_and_src.scpt"
defaults write /Users/"$myuser"/Library/Preferences/nl.syncfactory.Hedge.Mac.plist SFIDefaultsDiskBusyScript "/Library/OffShootConfig/AppleScripts/clear_dest_and_src.scpt"
sleep .5
echo " Setting SFIDefaultsFileCopyCompletedScript to renameMHLAfterCopy.scpt"
defaults write /Users/"$myuser"/Library/Preferences/nl.syncfactory.Hedge.Mac.plist SFIDefaultsFileCopyCompletedScript "/Library/OffShootConfig/AppleScripts/renameMHLAfterCopy.scpt"
sleep .5
echo "OffShoot Will Open With All Scripts and Settings Configured"
open 'offshoot://activate?key=????????'
I affectively tried to put my script into the script in the link above. But when I run the policy from Self Service, it just wheels forever. Never completes.
#!/bin/bash
loggedInUser=$(stat -f%Su /dev/console)
loggedInUID=$(id -u "$loggedInUser")
if [[ "$loggedInUser" != "root" ]] || [[ "$loggedInUID" -ne 0 ]]; then
cat << EOF > /private/tmp/script.sh
#!/bin/bash
echo "OffShoot Will Open To Create nl.syncfactory.Hedge.Mac.plist"
open /Applications/OffShoot.app
sleep 2
#Kill OffShoot
ps -ef | grep OffShoot | grep -v grep | awk '{print $2}' | xargs kill
sleep .5
#Set OffShoot Scripts and Open OffShoot
echo " Setting SFIDefaultsFirstAppStart to - False"
defaults write /Users/"$loggedInUser"/Library/Preferences/nl.syncfactory.Hedge.Mac.plist SFIDefaultsFirstAppStart -string "False"
sleep .5
echo " Setting SFIDefaultsUserAcceptsResponsibilityForScripts - True"
defaults write /Users/"$loggedInUser"/Library/Preferences/nl.syncfactory.Hedge.Mac.plist SFIDefaultsUserAcceptsResponsibilityForScripts "True"
sleep .5
echo " Setting SFIDefaultsHedgeStartedScript to callFilmsAPIOnOpen.scpt"
defaults write /Users/"$loggedInUser"/Library/Preferences/nl.syncfactory.Hedge.Mac.plist SFIDefaultsHedgeStartedScript "/Library/OffShootConfig/AppleScripts/callFilmsAPIOnOpen.scpt"
sleep .5
echo " Setting SFIDefaultsDiskBusyScript to clear_dest_and_src.scpt"
defaults write /Users/"$loggedInUser"/Library/Preferences/nl.syncfactory.Hedge.Mac.plist SFIDefaultsDiskBusyScript "/Library/OffShootConfig/AppleScripts/clear_dest_and_src.scpt"
sleep .5
echo " Setting SFIDefaultsFileCopyCompletedScript to renameMHLAfterCopy.scpt"
defaults write /Users/"$loggedInUser"/Library/Preferences/nl.syncfactory.Hedge.Mac.plist SFIDefaultsFileCopyCompletedScript "/Library/OffShootConfig/AppleScripts/renameMHLAfterCopy.scpt"
sleep .5
echo "OffShoot Will Open With All Scripts and Settings Configured"
open 'offshoot://activate??????'
exit 0
EOF
else
echo "No user logged in. Can't run as user, so exiting"
exit 0
fi
if [ -e /private/tmp/script.sh ]; then
/bin/chmod +x /private/tmp/script.sh
/bin/launchctl asuser "$loggedInUID" sudo -iu "$loggedInUser" "/private/tmp/script.sh"
sleep 2
echo "Cleaning up..."
/bin/rm -f "/private/tmp/script.sh"
else
echo "Oops! Couldn't find the script to run. Something went wrong!"
exit 1
fi
Posted on 09-18-2024 11:31 AM
Posted on 09-18-2024 11:33 AM
So, in your first script, the open commands are running as root. You could try and run them as the user. I would reference this article:
https://scriptingosx.com/2020/08/running-a-command-as-another-user/
to create the function that runs the command as the user, so, in your script you could do:
runAsUser open /Applications/Offshoot.app
to run the application in the user context. You could also do the same with the defaults commands to make sure they are running in user context