Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

AlexR-Hfx

macrumors newbie
Original poster
Jan 29, 2015
5
0
I'm trying to run a logout script to empty users' trash (and am planning to add to it later to try to whittle down the size of some of the Google caches). But I can't get the script to run. I have read the Apple article that says Login/Logout hooks are depreciated, but they should still run.

I created a logoutscript.sh file in /Library/Scripts, and set permissions to allow all to execute. The script does empty the trash when I manually run it in Terminal, whether logged in as admin, a standard user, or guest.

I used
Code:
sudo defaults write com.apple.loginwindow LoginHook
to create the hook and looked at the
Code:
/var/root/Library/Preferences/com.apple.loginwindow.plist
file to verify that the hook was created.

All seems to be good, but it doesn't run. Help, please!
 
It might be a typo or you simply forgot to include it but the syntax is :

Code:
sudo defaults write com.apple.loginwindow LoginHook /path/to/script

Use the same procedure to add or remove a logout hook, but type LogoutHook instead of LoginHook.

Your example doesn't use /path/to/script or LogoutHook.
 
Thanks. I did include the correct path and LogoutHook in my defaults write. And I checked that the plist was created. To be complete, here is the contents of /var/root/Library/Preferences/com.apple.loginwindow.plist:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>LogoutHook</key>
	<string>/library/scripts/logoutscript.sh</string>
	<key>NSWindow Frame ProcessPanel</key>
	<string>787 569 346 293 0 0 1920 1057 </string>
	<key>TALLogoutReason</key>
	<string>Shut Down</string>
</dict>
</plist>

Sorry to leave that out of the original post!
 
Please post the output from the following commands:
Code:
ls -ld /Library/scripts/
ls -l  /Library/scripts/logoutscript.sh

Copy and paste from the Terminal window into a reply. Accuracy is important.

Also, post the complete script.
 
My logoutscript.sh:
Code:
#! /bin/bash
rm -rf ~/.Trash/*

In Terminal, ls -ld /Library/scripts/ produces:
Code:
drwxr-xr-x  11 root  wheel  374 26 Jan 14:05 /Library/scripts/

and ls -l /Library/scripts/logoutscript.sh produces:
Code:
-rwxr-xr-x@ 1 root  wheel  30 26 Jan 14:23 /Library/scripts/logoutscript.sh
 
My logoutscript.sh:
Code:
#! /bin/bash
rm -rf ~/.Trash/*

In Terminal, ls -ld /Library/scripts/ produces:
Code:
drwxr-xr-x  11 root  wheel  374 26 Jan 14:05 /Library/scripts/

and ls -l /Library/scripts/logoutscript.sh produces:
Code:
-rwxr-xr-x@ 1 root  wheel  30 26 Jan 14:23 /Library/scripts/logoutscript.sh

The 'ls' output looks ok, but the use of ~ in the script concerns me.

Login and logout scripts run as root (see the reference docs linked in post #1). This may mean that HOME is set to root's home or at least not to the specific user's home. If HOME is for root, then ~/.Trash/* won't be what you want.

You need to discover what ~ evaluates to when the script is run at logout.

A simple way to get that is to change the script to echo ~/ to a shared file somewhere. E.g.:
Code:
echo ~/ >>/Users/Shared/logout_twiddle.txt
If HOME and ~ refer to root, then also check $1. According to the docs, that's the short username being logged in or out. E.g.:
Code:
echo "twiddle:" ~/ >>/Users/Shared/logout_twiddle.txt
echo "args:" "$@" >>/Users/Shared/logout_twiddle.txt
You could also use the 'set' command to list all the shell vars, again appending to a shared file.

All the above comes from the first rule of debugging: Confirm Your Expectations.
 
Thanks, chown33, this is (I think) putting me on the right track. I had read that scripts ran as root, but thought that just meant with root permissions.

When I ran
Code:
echo ~/ >>/Users/Shared/logout_twiddle.txt

in my logoutscript I got
Code:
var/root

When I ran
Code:
echo /Users/$1/.Trash/* >>/Users/Shared/logout_twiddle.txt

I got instead
Code:
/Users/alexr/.Trash/*

But when I run the following in my logoutscript the trash is not emptied.
Code:
#! /bin/bash
rm -rf /Users/$1/.Trash/*

What am I missing?

I'm very new to shell scripting, and very much appreciate your help!
 
Put the following in your logoutscript. Post the complete actual output from the resulting file.
Code:
id  >>/Users/Shared/logouts.txt
ls -ld /Users/"$1"/.Trash/*  >>/Users/Shared/logouts.txt
ls -la /Users/"$1"  >>/Users/Shared/logouts.txt

It's entirely possible your per-user trash is empty. There's more than one trash folder on a disk. You might be looking in the wrong one.
 
Chown33, you have pointed me far enough in the right direction so that I finally saw what should have been obvious: my user accounts are on a network volume and I needed to prefix the path with /Network/Servers/myservername, e.g.
Code:
rm -rf /Network/Servers/myservername/Students/"$1"/.Trash/*

The script now works as expected. The key was being able to debug the script using >> to append output to a file. That's handy! Many thanks!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.