There have been many times where I thought to myself — I wish my computer would do X when it goes to sleep and Y when it wakes up. There are many applications for this, some include:
- Mounting an SSH filesystem depending on WiFi connection
- Have the computer greet you and dismiss you (very nerdy)
- Disconnect/Kill some application that always hangs on sleep
- Move files from one folder to another
- Tweet
So how is this nerdy matter accomplished? It’s actually pretty trivial. Familiar with bash? Then you’re on the way to sleep/wake success!
Installing Sleepwatcher
Head over to http://www.bernhard-baehr.de/ and download a copy of Sleepwatcher 2.2. The installation instructions are pretty simple:
$ sudo mkdir -p /usr/local/sbin /usr/local/share/man/man8 $ sudo cp ~/Desktop/sleepwatcher_2.2/sleepwatcher /usr/local/sbin $ sudo cp ~/Desktop/sleepwatcher_2.2/sleepwatcher.8 /usr/local/share/man/man8
Once installed, run it in daemon mode:
$ /usr/local/sbin/sleepwatcher -d --sleep /path/to/your/sleepscript --wakeup /path/to/your/wakeupscript
If testing is needed run it in verbose mode:
$ /usr/local/sbin/sleepwatcher --verbose --sleep /path/to/your/sleepscript --wakeup /path/to/your/wakeupscript
A simple hello/goodbye script
Now that sleepwatcher is installed, let’s make the computer say Welcome back, <yourname> on wake and Goodbye on sleep. Two files are needed for this:
- sleepscript – script that runs when the computer sleeps
- wakescript – script that runs when the computer wakes
The scripts can be placed anywhere but the standard place would be to place them in /Users/<username>/bin. The bin folder should contain:
/Users/<username>/bin/sleepscript/Users/<username>/bin/wakescript
The scripts should contain:
Sleep Script
function say_goodbye {
say -v Vicki "goodbye"
}
say_goodbye
Wake Script
function say_hello {
say -v Vicki "Welcome back, username"
}
say_hello
Make sure to chmod the scripts to 775. Next, launch the daemon to test:
$ /usr/local/sbin/sleepwatcher -d --sleep /Users/<username>/bin/sleepscript --wakeup /Users/<username>/bin/wakescript
Demo
Here is a video demo showing my Macbook Air saying “Welcome back” and “Goodbye”. 1337!
Happy Coding!
