Posted on 04-19-2016 06:55 AM
I'm stumped. I've got a very basic script that is designed to delete Google Chrome from /Applications and ~/Applications (if present). When I run this locally as sudo delete_chrome.sh it works just fine and it does exactly what it is supposed to do. It also works when run from a Policy in Self Service. When it is run via policy at login it only deletes /Applications/Google Chrome.app It doesn't seem to touch the user's Applications in his home folder.
#!/bin/sh
#variable for storing the current users name
currentuser=`stat -f "%Su" /dev/console`
# delete /Applications/Google Chrome.app and ~/Applications/Google Chrome.app
rm -Rf "/Applications/Google Chrome.app/"
if [ -e "/Users/$currentuser/Applications/Google Chrome.app/" ]
then
rm -Rf "/Users/$currentuser/Applications/Google Chrome.app/"
fi
The only thing that I can think of is that it doesn't recognize that the user is logged in yet. Could this be the cause? Is there a way to make it recognize who is logged in during the execution of login policies?
Posted on 04-19-2016 07:17 AM
Missing a 'fi' at the end of your script.
Posted on 04-19-2016 08:38 AM
@AVmcclint you can add in a echo "$currentuser" in the script and then look at the log in Policy logs to see whats returning. My guess is it might be returning loginWindow instead of the username you are expecting.
Posted on 04-19-2016 08:54 AM
@thoule oops. My copy/paste missed the last fi but it is there in the live script. I'll fix that in the post.
@ddcdennisb I'll try that to see what it reports.
Does anyone else have scripts that run at login and refer to the current user?
Posted on 04-19-2016 08:56 AM
I have a few. some old scripts and new ones. I get current user either by:
un=`ls -l /dev/console | cut -d " " -f4`
or by
loggedInUser=`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 + "
");'`
Posted on 04-19-2016 10:02 AM
Do you need to find the current logged in user? You appear to want to remove each existence of the app.
find /Users/*/Applications -maxdepth 1 -name "Google Chrome.app" -exec rm -R {} ;
or search the whole of Users
find /Users -name "Google Chrome.app" -exec rm -R {} ;
or you could use mdfind
mdfind -onlyin /Users kMDItemFSName="Google*Chrome.app"
If you really want to find user at login, there are so many threads already on here regarding this. If you use a login policy then $3 is the user logging in.
You are almost certainly right that this is trying to run before the user logs in and at that point console belongs to root, so you will be trying to remove
/Users/root/Applications/Google Chrome.app/
Which probably doesn't exist.
Posted on 04-19-2016 10:08 AM
The very simple way would be to use $3 instead of $currentuser. This will be fine if you're only running it at login.
Posted on 04-25-2016 06:20 AM
@ddcdennisb I inserted echo $currentuser into the script and it reported "root". I ended up changing the script to do
find /Users/*/Applications -maxdepth 1 -name "Google Chrome.app" -exec rm -R {} ;
instead and that seems to accomplish my goal.
Posted on 05-11-2016 11:27 AM
No wonder I've never had luck with $3. I had no idea it only applied during login. I've been tinkering with my scripts and I think I'm going to have to build every script I have in a way that includes elements of this so they can apply at login and in Self Service without having to write separate script files.
#!/bin/sh
#variable for storing the current users name
currentuser=`stat -f "%Su" /dev/console`
# these are just to confirm what username is being registered at the time of execution.
# it helps me troubleshoot as well because I can see the output in the policy log.
echo $currentuser
echo $3
if [ $currentuser = "root" ]
then
rm -rf /Users/$3/Library/blah/blah/blah
touch /Users/$3/Library/blah/blah/blah
whatevercommandyouneedtorun /Users/$3/path/within/home/folder/
else
# same commands as above but with the other variable
rm -rf /Users/$currentuser/Library/blah/blah/blah
touch /Users/$currentuser/Library/blah/blah/blah
whatevercommandyouneedtorun /Users/$currentuser/path/within/home/folder/
fi
Here are results of running a policy in Self Service:
Running script test.sh...
Script exit code: 0
Script result: cmcintosh
cmcintosh
And results of running at login:
Running script test.sh...
Script exit code: 0
Script result: root
cmcintosh
The IF...THEN...ELSE of the script properly determined which set of commands needed to be run at that particular moment. I still need to keep an eye on the $3 component of this method because I've had many other occasions where $3 reported "root" at login. But so far this has better results in my testing than only using $3.