Script Not working in JSS but will work via ARD

tcarlson
New Contributor III

Hi I have simple command lines that I've used to clean up our Lab based mac minis, deleting files deleting files in the following locations.

rm -rf ~/Desktop/
rm -rf ~/Downloads/

rm -rf ~/Applications/
rm -rf ~/.Trash/

I can run it them all at once with ARD with no problem, when I save it as a script on the JSS it will error out. I'm not a scripting guru by any stretch, any info would be helpful. I've tried it with and without #!/bin/bash . 9e0cf8a6fde846d1a807cbb509e3be20

5 REPLIES 5

cbrewer
Valued Contributor II

~ specifies the current users home directory location. Jamf scripts run as root.

You're gonna need a script that has more logic depending on whether you want to delete data for a specific user or all users. Jamf sets the current logged in user as the variable $3. If you want to run this as a login or logout script, you can put $3 in your rm path. Then just be careful which users you scope it to.

#!/bin/sh
rm -R /Users/$3/Desktop/*

tomt
Valued Contributor

What user account are you trying to remove these folders from?

SeanA
Contributor III

taking @tomt's comment a step further, when you run these Terminal commands in ARD, what account do you run the rm commands from?

2c156957384f4c768472e291da430e8b

tcarlson
New Contributor III

Yes @SeanA , I was running as current console user, makes sense why it wasn't working. Works good now. Thank you for the responses.

pcrandom
Contributor

I combined a few methods to best determine the currently logged in user, and if not available, the most likely "primary" user into this script:

#!/bin/bash

# Below is Apple's recommended way, which will supposedly only return the active user, even if multiple users are logged in.  (Blank if no user is logged in.)

userMethod1="$(python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "
");')"

# Below is a more common way of getting current user. May return multiple names if FUS is enabled and more than one user is logged in.  ("root" if no user is logged in.)

userMethod2="$(stat -f%Su /dev/console)"

# Below is a method to determine most likely primary user based on number of logins to the console.

userMethod3=$( last -t console | awk '{print $1}' | sort | uniq -c | sort -n | awk 'END{print $NF}' )

if [ "$userMethod1" == "" ] || [ "$userMethod2" == "root" ]; then
    loggedInUser="$userMethod3"
else
    loggedInUser="$userMethod1"   
fi

Note that Method 2 is actually extraneous if you use Method 1, but I just kept it in for reference.

You could then use $loggedInUser in place of the username, or you could run commands as the logged in user. I suppose these commands should work after the variable is assigned:

sudo -u "$loggedInUser" rm -rf ~/Desktop/
sudo -u "$loggedInUser" rm -rf ~/Downloads/
sudo -u "$loggedInUser" rm -rf ~/Applications/
sudo -u "$loggedInUser" rm -rf ~/.Trash/