#!/bin/sh
osascript -l AppleScript -e 'tell Application "Finder" to say "Just going to sleep now, ok?"'
(Don't enter these lines ;-)
Save the file as ~/.sleep and wait about 5 minutes for your screensaver to launch. Listen.
That's it. The rest is only limited by imagination. For starters, study the man page: man sleepwatcher Then open /Library/StartupItems/SleepWatcher/SleepWatcher in TextEdit and find the line that starts SleepWatcher. The man page suggests that you can do A LOT MORE than minimum, but the default configuration is powerful.
In my own .sleep file I launch a script to fire off the PostgreSQL dumpall command, if it hasn't run yet. Screensaver time is the perfect time to make a database backup, at least for small personal databases.