Basic Terminal Commands Not Working In Self Service

derek_ritchison
Contributor

Hey all- First time poster, and actually first time Jamf user. Literally, today was my first day learning it. I've been building our Self Service portal to roll out to users, and in an attempt to simply familiarize myself with it I've been using some very simple Terminal commands as "Useful Tools" for end users to click and run. However, some are simply not working even though when I run the exact same command locally, via Terminal, they will work.5665f0a1cf10454f82c8f5d110750282

I've attached a screenshot of a simple command that would change the file type of a screenshot. When I run the command via Terminal, it works. When I run the command via my Self Service app, it does not. Ideas?

4 REPLIES 4

kbingham
New Contributor III

You need to add the shebang in scripts

#!/bin/sh

defaults write com.apple.screencapture type jpg

mm2270
Legendary Contributor III

The shebang isn't actually the issue believe it or not. I have some older scripts in our JSS that don't have them and they run fine, since Jamf Pro understands they are scripts and assumes bash if it doesn't see a specific interpreter.

The problem is you cannot run defaults commands that don't use the full path to a plist, or are not run by the user (scripts run as "root" by default) and expect them to work on the user space. When you find defaults commands online that use the syntax defaults write com.org.domain somesetting these are expected to be run as the user. Since the script runs as root, it doesn't do what you expect since it's actually setting the screencapture setting (in this instance) for root instead.

You can solve this by capturing the username and directing the defaults command to run as the user

#!/bin/sh

currentuser=$(stat -f%Su /dev/console)
sudo -u $currentuser defaults write com.apple.screencapture type jpg

or, capture the username and direct the defaults command at the user's plist file and then correct permissions on the plist

#!/bin/sh

currentuser=$(stat -f%Su /dev/console)
defaults write /Users/$currentuser/Library/Preferences/com.apple.screencapture.plist type jpg
chown $currentuser /Users/$currentuser/Library/Preferences/com.apple.screencapture.plist

Edit: Meant to say that it is best to always include a shebang in any script, as it removes any guesswork and potential problems that can arise from not having them there, so I do think you should include them. Just saying that even WITH the shebang, it won't solve the main problem.

derek_ritchison
Contributor

Thank you both so much. I will try this today on some of my bash scripts!

bentoms
Release Candidate Programs Tester

@derek.ritchison Have a look at this.

It's similar to what you're trying to do, so might help. :)