Run Script Without Sudo

miotke
New Contributor III

Hello again!

I'm trying to install our company's app to the Xcode iOS simulator via a script that essentially just runs xcrun simctl install booted [Path and App] however, as I'm finding out Jamf runs every command/script as sudo which is breaking this command. If I run sudo xcrun simctl install booted [Path and App] just in the command line I'm told No devices are booted. but the simulator is booted and I'm staring right at it.

I can always create a new package by installing Xcode then our app to the simulator, however that package ends up being over 6GB and at the end of the day not efficient. So we want to install the new version of the app in the existing simulator.

Is there a way to tell Jamf not to run the command with sudo?

Here's what I've tried so far:
- Write a script that just runs xcrun simctl install booted [Path and App] - this works in the command line but fails when pushed out via policy.
- Write a non-compiled AppleScript that runs xcrun simctl install booted [Path and App] - Script Editor runs the script and it works, fails with the No devices are booted. error via policy.
- Created a policy with the Files and Processes payload with xcrun simctl install booted [Path and App] as the Execute Command but again, it runs as root and I get No devices are booted..

Thanks for any help!

1 ACCEPTED SOLUTION

mm2270
Legendary Contributor III

Try

#!/bin/bash

loggedInUser=$(stat -f%Su /dev/console)
loggedInUID=$(id -u $loggedInUser)

/bin/launchctl asuser $loggedInUID sudo -iu $loggedInUser /usr/bin/xcrun simctl install booted [Path and App]

View solution in original post

5 REPLIES 5

curullij
Contributor

The issue may be that the script is being as root.

What you can do is get the username of the logged in user currentuser=ls -l /dev/console | cut -d " " -f4

then run your command with
su - "${currentuser}" -c xcrun simctl install booted [Path and App]

That might work? I'm not in front of my mac at the moment so I can't test it

miotke
New Contributor III

Thanks for the help @curullij. I get an error of Script result: su: unknown login:

Here's the command su - "${currentLoggedInUser}" -c xcrun simctl install booted [Path and App]

timdambrosio
Contributor

Try this if the script is run via JAMF

su $3 -c xcrun simctl install booted [Path and App]

mm2270
Legendary Contributor III

Try

#!/bin/bash

loggedInUser=$(stat -f%Su /dev/console)
loggedInUID=$(id -u $loggedInUser)

/bin/launchctl asuser $loggedInUID sudo -iu $loggedInUser /usr/bin/xcrun simctl install booted [Path and App]

miotke
New Contributor III

Thanks @mm2270, that totally worked!

Also, thank you everyone for your input and helping come to the solution!