Benches macOS

7

logout hooks for dummies

apple Verified

Imported from the Apple Support Forums via the Wayback Machine 🔗

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.

7 replies

Sign in or join Beachball Help to reply.

  1. 0

    biovizier

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    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. 0

    red_menace

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    You could just cheat and use LoginWindow Manager.

  3. 0

    V.K.

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    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. 0

    V.K.

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    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. 0

    red_menace

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Bombich also has some information on their website about running shell scripts at login and logout.

  6. 0

    biovizier

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    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. 0

    V.K.

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    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!