7 Replies Latest reply: Mar 22, 2008 9:30 PM by V.K.
V.K. Level 9 Level 9 (56,120 points)
Could somebody please explain in layman's terms how to set up logout hooks?
I found several recipes on the web all suggesting modifying the loginwindow plist but none of this has worked for me.

PowerBook G4 17'', 1.5GHz, Mac OS X (10.5.2), Mac Pro 2.66GHz, Mac Pro 3.2GHz
  • 1. Re: logout hooks for dummies
    biovizier Level 5 Level 5 (7,925 points)
    Actually, the method of configuring "logout hooks" depends on the OS X version - Apple keeps moving it around so a method for one version may not work on another.

    Since 10.4, the login window hooks go in the "com.apple.loginwindow.plist" file in the "root" account's "Preferences" folder (i.e. "/private/var/root/Library/Preferences"). The property name is 'LoginHook' or 'LogoutHook" respectively for the two types (both are case-sensitive), and the value should be the full path to the script.

    Using the 'defaults' command as "root" is probably the easiest way to set them up, as in:<pre>
    sudo /usr/bin/defaults write com.apple.loginwindow LogoutHook /path/to/script</pre>
    http://docs.info.apple.com/article.html?artnum=301446

    Note that loginwindow hooks run as "root" so the script should be stored in a location such that its entire path is write protected against anyone other than "root". It is also probably a good idea to make sure the script works correctly in "Terminal.app" before letting it run as "root" automatically as a hook.
  • 2. Re: logout hooks for dummies
    red_menace Level 6 Level 6 (14,615 points)
    You could just cheat and use LoginWindow Manager.
  • 3. Re: logout hooks for dummies
    V.K. Level 9 Level 9 (56,120 points)
    Thanks.
    biovizier wrote:
    Actually, the method of configuring "logout hooks" depends on the OS X version - Apple keeps moving it around so a method for one version may not work on another.

    I run 10.5.2. does this make a difference to what you suggest?

    Since 10.4, the login window hooks go in the "com.apple.loginwindow.plist" file in the "root" account's "Preferences" folder (i.e. "/private/var/root/Library/Preferences"). The property name is 'LoginHook' or 'LogoutHook" respectively for the two types (both are case-sensitive), and the value should be the full path to the script.

    Using the 'defaults' command as "root" is probably the easiest way to set them up, as in:<pre>
    sudo /usr/bin/defaults write com.apple.loginwindow LogoutHook /path/to/script</pre>
    http://docs.info.apple.com/article.html?artnum=301446

    Note that loginwindow hooks run as "root" so the script should be stored in a location such that its entire path is write protected against anyone other than "root". It is also probably a good idea to make sure the script works correctly in "Terminal.app" before letting it run as "root" automatically as a hook.





    This is exactly what i tried earlier except I used

    sudo defaults write com.apple.loginwindow LogoutHook /path/to/script

    isn't that the same thing? I tried out the script first and it works. I'll try again with your command.

    Message was edited by: V.K.
  • 4. Re: logout hooks for dummies
    V.K. Level 9 Level 9 (56,120 points)
    thanks! Somehow I missed that capability of the LoginWindow manager - I did come up on it googling around. Still, I would be more satisfied if i could learn how to do it by hand.
  • 5. Re: logout hooks for dummies
    red_menace Level 6 Level 6 (14,615 points)
    Bombich also has some information on their website about running shell scripts at login and logout.
  • 6. Re: logout hooks for dummies
    biovizier Level 5 Level 5 (7,925 points)
    Yes, the commands are more or less equivalent. There are some common gotchas - in the 'defaults' command, include the full path, and don't escape spaces, etc. - rather quote the whole thing.

    Also, since the "hook" always runs as "root", be aware that references to "~" will always point to the "root" account's home folder. I'm not sure what root's 'PATH' is by default either, so make sure all commands in your script include the full path (you should do this anyway for security reasons for commands running as "root").

    There are probably others, but these issues seem to come up fairly frequently...
  • 7. Re: logout hooks for dummies
    V.K. Level 9 Level 9 (56,120 points)
    crud! that was it! I decided to first test the hook on something really simple and inoffensive so I had it run
    rm -rf ~/.Trash/*

    No wonder nothing happened.
    Thanks!