web analytics

Creating Apple Login and Logout Scripts to Track Computer Usage in Console System Log Files

Technology > Apple

Summary. This document describes how to create a system file that will track when users login and logout. The resulting log file can be imported into a spreadsheet for calculating the amount of time spent working on the computer. These instructions are for experienced users only. Proceed at your own risk.

Alternative. An alternative to this method is to use the existing system.log file from the Apple Console program, as described in the document Using Apple Mac Console Event Log Files to Report Login and Logout Times for Calculating Computer Usage.

Source. The source material for this document is located on MacWorld.com as originally described by user regulus. Changes have been made to those original instructions. For example, the chown command was replaced with the chmod command, since the chown command isn’t recognized by Apple Snow Leopard. The timestamp is configured instead as date and time for easier importing into Microsoft Excel. The output data has the date and time first. Additional tips are also given to assist with creating the login/logout tracking files and placing them where they belong.

1. Download TextWrangler by BareBones.com from the TextWrangler page on the Download.com website. You will use this program to create the files described below.

2. Open TextWrangler and begin with a blank file. Copy and paste the following text into that file. Replace any smart quotes with standard quotes using a smart quote remover utility. Then save the file as loginscript directly to your hard drive (not in any sub folder, but in the root directory). Note that the timestamp format below has been modified to be in a format compatible for later importing into Microsoft Excel.

#!/bin/bash
# login script

username=${1}
date=`date ”+%m-%d-%Y”`
time=`date ”+%H:%M:%S”`
computer=`hostname`
logfile=”/var/log/usertracking.log”

echo $date” “$time,” “$username,” “$computer,” login” >> “$logfile”

3. Copy and paste the following text into a new file. Replace any smart quotes with standard quotes. Then save the file as logoutscript directly to your hard drive (not in any sub folder, but in the root directory). Note that the timestamp format below has been modified to be in a format compatible for later importing into Microsoft Excel.

#!/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”

4. Start the Terminal program (found under Applications > Utilities) and at the command prompt type cd / and then press Enter. This should take you from your home directory to the top level of your hard drive.

5. Type sudo mv loginscript /usr/local/bin then press Enter. This will move the loginscript file into the bin directory.

6. Type sudo mv logoutscript /usr/local/bin then press Enter. This will move the logoutscript file into the bin directory.

8. Type sudo chmod a+x /usr/local/bin/loginscript then press Enter. This will give the loginscript file the authority to run on your computer.

9. Type sudo chmod a+x /usr/local/bin/logoutscript then press Enter. This will give the logoutscript file the authority to run on your computer.

10. Type sudo defaults write com.apple.loginwindow \ then press Enter. When prompted, type LoginHook /usr/local/bin/loginscript and then press Enter to return to the standard Terminal prompt.

11. Type sudo defaults write com.apple.loginwindow \ then press Enter. When prompted, type LogoutHook /usr/local/bin/logoutscript and then press Enter to return to the standard Terminal prompt.

Viewing and Saving. The log file will be created once someone logs out and then logs back into the computer. To view the usertracking.log file, open the Console application (found in Applications > Utilities) and go to FILES > /private/var/log > section and find the usertracking.log file listed alphabetically toward the bottom of the list. Once you click on the log file, in the right window you’ll see its contents. From the File menu, you can choose Save Copy As and store a copy of the log file on your computer.

Editing Log Files. It’s not necessarily advisable to edit log files, but if for some reason you are making modifications of your own to the above output scripts, and you want to remove some data from the usertracking.log file, you can right click on that file in Console, then choose to Show in Finder. Then, right click on the usertacking.log file in finder and open it with TextWrangler.

Analysis. The usertracking.log file can be saved and then opened with a spreadsheet like Microsoft Excel for further analysis. Once in a spreadsheet, the differential between start and stop times can be calculated, providing statistics such as total weekly usage.

Removal. If you want to remove the changes made above, follow these steps:

1. Type sudo defaults delete com.apple.loginwindow LoginHook then press Enter.

2. Type sudo defaults delete com.apple.loginwindow LogoutHook then press Enter.

3. Type sudo rm /usr/local/bin/loginscript then press Enter.

4. Type sudo rm /usr/local/bin/logoutscript then press Enter.

5. Type sudo rm /var/log/usertracking.log then press Enter.

By Greg Johnson

Greg Johnson is a freelance writer and tech consultant in Iowa City. He is also the founder and Director of the ResourcesForLife.com website. Learn more at AboutGregJohnson.com