managing young computer users

My young users have some access to the wonders of the cybernetic world, but on a time-limited basis. As you might imagine if you ever were or have a child, getting them to quit when the time comes is a chore.

Turns out Mac OS X has some hooks to let you manage that without having to get involved.

Apple supports login and logout hooks that allow actions to be tied to login and logout events, as olde skoole users might have used in the UNIX shell.

The first two steps are not optional, but pretty well documented. You need to edit /etc/ttys and make some changes to the system defaults.

The change to /etc/ttys is here: I have left the original line in the file, commented out, in case I need to roll back. So look for the second line (without the # in column 1) and add the first line. Make sure you don’t add any hard returns or newlines (I think pico does so be careful if you use that).


#console "/usr/libexec/getty std.57600" vt100 on secure
console "/System/Library/CoreServices/loginwindow.app/Contents/MacOS/loginwindow -LoginHook /usr/local/bin/loginscript" vt100 on secure
#console "/System/Library/CoreServices/loginwindow.app/Contents/MacOS/loginwindow" vt100 on secure onoption="/usr/libexec/getty std.9600"

Then you need to decide what to call your login script (/usr/local/bin/loginscript seems like a good choice). Replace the obvious placeholder with that and run that command.

sudo defaults write com.apple.loginwindow LoginHook /path/to/script

So far so good.

The loginscript or hook is simple:

#!/bin/bash
# login script

username=${1}
if [ $username = "mom|dad" ]; then
exit
else
exec /usr/local/bin/timeout.sh &
fi

NB: scripts or executables that run as loginhooks block the rest of the user’s session until they complete. So the actual timing and warning stuff takes place in the script called by the loginhook. Substitute unfettered usernames as needed.

And this is the script itself. It’s a first cut, so there are some optimizations that could be made (some refactoring into functions is obvious) but it works as it is. It’s a mélange of bash and applescript, so I am not sure how you replicate this elsewhere.

Take your own best hold on it and see what you come up with.

#/bin/bash
username=`/usr/bin/whoami`
/usr/bin/logger $username logged in
/usr/bin/osascript -e "set Volume 2"
/usr/bin/say "you have 25 minutes. Enjoy"
/bin/sleep 900
/usr/bin/osascript -e "set Volume 2"
/usr/bin/say "5 minutes left"
/bin/sleep 240
/usr/bin/osascript -e "set Volume 2"
/usr/bin/say "1 minute left"
/bin/sleep 60
/usr/bin/say "buh bye now"
/usr/bin/osascript -e 'tell application "System Events" to log out'
/usr/bin/logger $username logged out

There are lots of resources on this, Google can help you find them, but this example should serve as a good launching point for your own experiments. Leave any tips or gotchas in comments: I’m sure there are more improvements than I can see.

Leave a Reply

Your email address will not be published. Required fields are marked *