10.6: Fix sleep problems with SleepWatcher

Dec 02, '10 07:30:00AM

Contributed by: shinew7911

Snow Leopard only hintI built a customized Mac and was very happy with it, until I realized that it wouldn't automatically go to sleep regardless of my Energy Saver settings in System Preferences.

I did some research online and noticed that many people with real Macs were having similar issues (in my case it's because of the DVD drive), and then I found this site with a link to SleepWatcher; a very good utility!

Unfortunately, all the hints mentioned here either did not work for me, were incomplete, needed some other utility to work, or had an outdated method for launching.

So I've detailed the steps below to make it work system wide (and no it won't go to sleep if you're watching a movie) and hope it'll help those of you who have an insomniac Mac for whatever reason.

Note: this tip assumes that your system has no issue manually entering sleep and waking back up from it.

My Solution:

1 -- Install Homebrew. You need to have the Developer Tools installed to compile this.

Besides many other reasons to have Homebrew on your computer, it is one of the easiest way to install command line software. Please use the default choices if the installation asks you to choose unless you know what you're doing. To install, copy and paste the following into your Terminal and then press Enter:

ruby -e "$(curl -fsSLk https://gist.github.com/raw/323731/install_homebrew.rb)"
2 -- Install SleepWatcher.

Enter the following in Terminal and type your root password when prompted:
sudo brew install sleepwatcher
3 -- Create the launchd plist.

You can just write your own or copy mine below, which is setup to go to sleep 15 minutes after the display is off. Just open up your favorite text editor and paste the following, and then name and save it to /Library/LaunchDaemons/com.cellar.sleepwatcher.plist.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "
http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.cellar.sleepwatcher</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/sbin/sleepwatcher</string>
               <string>-V</string>
               <string>-S /usr/local/sbin/sleepwatcher -d -t9000 -i
"/usr/local/sbin/sleepwatcher -n" -p /tmp/sleepwatcherid</string>
               <string>-W kill `cat /tmp/sleepwatcherid`</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
</dict>
</plist>
To set your own sleep timer after the display goes to sleep, you can modify the option in the plist where it starts with '-t' then followed by digits. The option uses tenth of a second increments, so to set a one hour idle time before system goes to sleep, you would use '-t36000', for half an hour, use '-t18000,' etc...

4 -- Set the permissions and enable SleepWatcher.

Go back to Terminal, enter the following and your root password when prompted.
sudo -i
cd /Library/LaunchDaemons
chown root:wheel com.cellar.sleepwatcher.plist
chmod 644 com.cellar.sleepwatcher.plist
launchctl load com.cellar.sleepwatcher.plist
logout
That's it! It should now be enabled and there is no need to reboot and it should remain working the next time you restart. To double check and see if it's running, enter the following in the Terminal:
sudo launchctl list | grep sleepwatcher
if you see com.cellar.sleepwatcher in the output, that means it's up and running.

Optional -- Uninstallation procedure

If you would like to uninstall SleepWatcher and Homebrew for whatever reason, enter the following in Terminal (authenticating when needed):
sudo -i
brew uninstall sleepwatcher
launchctl unload /Library/LaunchDaemons/com.cellar.sleepwatcher.plist
rm -f /Library/LaunchDaemons/com.cellar.sleepwatcher.plist
cd `brew --prefix`
rm -rf Cellar
brew prune
rm -rf Library .git .gitignore bin/brew README.md share/man/man1/brew
rm -rf ~/Library/Caches/Homebrew
logout


[crarko adds: I haven't tested this one. I don't endorse Hackintoshes so I removed most of the references to that, but a lot of standard Macs do experience sleep/power management issues from time to time, so having available tools to work on that is always handy. There is a lot going on in this hint so proceed with caution. If anyone does spot any errors in the code please let me know so I can correct it.]

Comments (10)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20101130192909425