Logout Hook not executing

Discussion in 'Mac Programming' started by AlexR-Hfx, Jan 29, 2015.

  1. AlexR-Hfx macrumors newbie

    Joined:
    Jan 29, 2015
    #1
    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!
     
  2. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #2
    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
    Your example doesn't use /path/to/script or LogoutHook.
     
  3. AlexR-Hfx thread starter macrumors newbie

    Joined:
    Jan 29, 2015
    #3
    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!
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    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.
     
  5. AlexR-Hfx thread starter macrumors newbie

    Joined:
    Jan 29, 2015
    #5
    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
     
  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #6
    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.
     
  7. AlexR-Hfx thread starter macrumors newbie

    Joined:
    Jan 29, 2015
    #7
    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!
     
  8. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #8
    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.
     
  9. AlexR-Hfx thread starter macrumors newbie

    Joined:
    Jan 29, 2015
    #9
    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!
     
  10. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #10
    Congrats on getting it working. I'm glad to help.
     

Share This Page