Archived - Mac OS X: Creating a login hook
Did you know that you can have Mac OS X run a script whenever you log in to your computer? You can if you create a "login hook." A login hook tells Mac OS X to execute a certain script when a user logs in. Unlike Startup Items that open when a user logs in, a login hook is a script that executes as root. This advanced article shows you how to set up a login hook.
With a login hook:
- The script specified as a login hook must be executable.
- The login hook will be run as root.
- In the login hook script, the variable $1 returns the short name of the user who is logging in.
- Other login actions wait until the hook has completely executed.
How to set up a login hook
Mac OS X 10.3, 10.4, or later
Note that with Mac OS X 10.3.x and 10.4.2 or later, you can use the alternative method at the bottom of this document instead, if you wish. For Mac OS X 10.4 and 10.4.1, you should always use the following steps:
- Open Terminal (Applications/Utilities).
- In the Terminal window, type:
sudo defaults write com.apple.loginwindow LoginHook /path/to/script
(where /path/to/script is the full path to the script that you want to execute when a user logs in—it doesn't have to be in the user's Home directory).
This modifies the /var/root/Library/Preferences/com.apple.loginwindow file.
- Type your password at the prompt, then press Return.
Mac OS X 10.2.x, 10.3.x, or 10.4.2 or later
- Open the /etc/ttys file: In the Finder, choose Go to Folder from the Go menu, type /etc/, then click Go.
- In the resulting window, open the ttys file in your preferred text editor (such as TextEdit).
- Look for a line that reads:
#console "/System/Library/CoreServices/loginwindow.app/Contents/MacOS/loginwindow" vt100 on secure window=/System/Library/CoreServices/WindowServer onoption="/usr/libexec/getty std.9600"
- Edit this line so that it reads as follows (there are no breaks in this line):
#console "/System/Library/CoreServices/loginwindow.app/Contents/MacOS/loginwindow -LoginHook /path/to/script" vt100 on secure window=/System/Library/CoreServices/WindowServer onoption="/usr/libexec/getty std.9600"
(where /path/to/script is the full path to the script that you want to execute when a user logs in).
- Save the file.
Be sure that the text editor you use to edit this file does not break the line above into more than one line. This method will also work in Mac OS X 10.3.