As a Mac administrator it can be useful to be run a shell script each time a user logs out. This allows a script to clean up temporary files, back up changes, and restore sane defaults for the next user.
Let's walk through how to schedule a shell script to run after a user logs out of Mac OS X. Our shell script is going to be very simple but you can make yours as complex as needs be.
Scheduling a Log Out Shell Script
- Launch System Preferences and select Power Manager.
- Click Add… to create a new event.
- Choose the template Run a script.
- Click Continue to the Script step.
Copy and paste your shell script into the Script step.
#!/bin/sh touch /tmp/lastlogout.txt
The shell script can be run as the root user, as a normal user, or even as whoever is currently logged in at the time. For this script, we have set the Environment to Super User (root).
- Click Continue to the When step.
- Adjust the time and date to the distant future; this trigger will be replaced in a later step.
Continue through the remaining steps until your event is created.
With these steps completed, you will have a basic Power Manager event queued up. This event includes your shell script but is not yet triggered after log out.
- Hold down the Option key on the keyboard and double-click on your event; the Event Editor will now appear.
- Delete the existing Date and Time trigger; use the Action cog and select Delete….
- Add a new Log Out trigger; use the Add a trigger pop-up menu and select Log Out.
- Click Save to save your changes and finish editing the event.
Your new Power Manager event is ready and will now run every time a user logs out.