Run Scripts At Login
Though still a standard practice on most Windows networks, Mac OS X administrators seldom create scripts to run at the beginning or the end of user sessions. The common problems they once solved, like resetting home directories or mounting network shares, can now be accomplished through server functionality or free third-party software. But what if you have an uncommon problem? This feature can be the perfect solution, allowing administrators to address unique issues in their individual environments.
The first step in this process is to use a text editor to create and save a shell script, essentially a list of Unix commands run as the root (or System) user by the LoginWindow process. In this context, the $1 variable can be used to represent the username of the account logging in (or out). The example below, for instance, simply deletes the contents of the user's Cache directory:
#!/bin/bash
rm -rf /Users/$1/Library/Caches/*
As always, you'll want to test your script thoroughly before deployment, as any command run as root has the potential to be destructive. If you're unfamiliar with scripting the bash shell, you can consult the Bash Reference Manual or Kirk McElhearn's excellent book The Mac OS X Command Line.
Once your script is complete, you'll need to set it as executable, marking it as a program that can be run by the Unix shell. You'll also want the script owned by root, so that only administrative users can edit it. You accomplish both from the Terminal, replacing PATH-TO-SCRIPT with the location of the script you’ve created:
sudo chmod 744 PATH-TO-SCRIPT
sudo chown root:wheel PATH-TO-SCRIPT
Login Scripts with OS X Server:
In a managed client environment using Open Directory, you can utilize Computer Lists to control features that are machine-specific. The lists are created in the "Accounts" pane of Workgroup Manager, then assigned functionality in the "Preferences" pane. Click the "Scripts" button, then select the shell scripts you'd like to run on login or log-out. In order to browse to them, the scripts must reside somewhere in the visible file system (rather than in the invisible Unix subsystem).
This approach requires two significant additional steps: First, the client machines must communicate with the server using "trusted directory binding", meaning that Open Directory is configured to communicate using the Kerberos security system. Second, each client machine must have remote login scripts enabled via the Terminal by typing:
sudo defaults write \
/var/root/Library/Preferences/com.apple.loginwindow \
EnableMCXLoginScripts -bool TRUE
Login Scripts on OS X Client:
Although running login scripts without a server infrastructure must be done entirely on the command line, it has the benefit of being a one-step process. With the scripts installed locally on each machine, open the Terminal and type:
sudo defaults write com.apple.loginwindow LoginHook PATH-TO-SCRIPT
If you'd like to run a log-out script as well (or instead), just replace LoginHook with LogoutHook when you enter the command.
Keep in mind that complex scripts can significantly effect login times. For this reason, running multiple tasks at log-out is less likely to prompt a flurry of troubleshooting calls. Used judiciously, this technique can be an ideal way to maintain a dependable user experience without sacrificing customization.
