Aug 18, '06 07:30:02AM • Contributed by: regulus
After reading about login hooks in OS X, and doing a little more research on the web, I came up with a few simple scripts that can be run at login and logout to solve my problem. With these scripts, any time anyone logs into or out of the computer, a log entry will be created. If you keep your log file in the /var/log directory, then you can easily view the file using the Console application.
So here's how I did it.
- Create the login and logout scripts as follows. Here's the login script:
And here's the logout script:#!/bin/bash # login script username=${1} timestamp=`date ''+%m-%d-%Y_%H:%M:%S''` computer=`hostname` logfile="/var/log/usertracking.log" echo $username," "$timestamp," "$computer," login" >> "$logfile"#!/bin/bash # logout script username=${1} timestamp=`date ''+%m-%d-%Y_%H:%M:%S''` computer=`hostname` logfile="/var/log/usertracking.log" echo $username," "$timestamp," "$computer," logout" >> "$logfile" - Save the scripts in /usr/local/bin as loginscript and logoutscript, respectively.
- Run the following Terminal commands to give the scripts execute permissions:
$ sudo chown a+x /usr/local/bin/loginscript $ sudo chown a+x /usr/local/bin/logoutscript - Run the following two Terminal commands to create the login and logout hooks:
$ sudo defaults write com.apple.loginwindow \ LoginHook /usr/local/bin/loginscript $ sudo defaults write com.apple.loginwindow \ LogoutHook /usr/local/bin/logoutscript
To remove these changes, type the following commands in Terminal:
$ sudo defaults delete com.apple.loginwindow LoginHook
$ sudo defaults delete com.apple.loginwindow LogoutHook
$ sudo rm /usr/local/bin/loginscript
$ sudo rm /usr/local/bin/logoutscript
$ sudo rm /var/log/usertracking.log
You can also track who logs into your computer via file sharing over your network, as explained in this hint. You can also completely disable the ability for guests to login via file sharing, as explained in this hint.
Good luck!
