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!

Hi Glen
Thanks for your very helpful tutorial on SleepWatcher. Without out your tutorial Terminal beginners like me would be stumped. And I wouldn’t have been able to fix the wake-every-2-hours issue that comes with enabling Wake for network access.
I have a question that i’d be grateful if you could answer
When I compare your tutorial to the the SleepWatcher 2.2 ReadMe [under the "Installation for new SleepWatcher users"], your tutorial stops before addressing points 5, 6 and 7.
Those points refer to configuring launchd for SleepWatcher. Would you be able to add those points to your tutorial and explain what they are for and why you didn’t cover them originally?
Am I right in thinking the way you have it set up, if I reboot my mac I’ll have to manually launch the SleepWatcher daemon again and launchd would automatically launch it?
Thanks again for the tutorial
Linda H
Oh and one more question, is the best way to stop the SleepWatcher daemon to use the command
sudo killall sleepwatcher
I stopped before 5, 6, and 7 because I assumed that if anyone wanted to start sleepwatcher on boot they could do it on their own. I was wrong! Thanks for the input. I’ll add those points to the tutorial and describe what is going on.
“Am I right in thinking the way you have it set up, if I reboot my mac I’ll have to manually launch the SleepWatcher daemon again and launchd would automatically launch it?”
Yes, you are correct in thinking the way I have it set up you have to re-enable sleepwatcher on boot. I initially enabled it that way on my computer because I was testing and not ready to have it launch on boot/login.
“Oh and one more question, is the best way to stop the SleepWatcher daemon to use the command ..”
Yes, that is a valid way to kill a daemon. If the daemon is designed correctly it looks for the SIGTERM posix command that is sent by killall or kill and will finish up its tasks before completely shutting down. Since sleepwatcher doesn’t have a start/stop command this is the correct way to stop the process.
“I’ll add those points to the tutorial and describe what is going on.”
That would be great. Thank you
Linda