Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

10.6: Fix sleep problems with SleepWatcher System 10.6
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.]
    •    
  • Currently 3.33 / 5
  You rated: 4 / 5 (6 votes cast)
 
[15,131 views]  

10.6: Fix sleep problems with SleepWatcher | 10 comments | Create New Account
Click here to return to the '10.6: Fix sleep problems with SleepWatcher' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.6: Fix sleep problems with SleepWatcher
Authored by: asmeurer on Dec 02, '10 09:27:22AM

This tool is also available through fink.



[ Reply to This | # ]
10.6: Fix sleep problems with SleepWatcher
Authored by: fds on Dec 02, '10 09:43:28AM

Looks like the point of the hint was thrown out along with the Hackintosh stuff. :)

I'm curious what sleep problems simply having SleepWatcher installed would fix. It's just a tool to run your own commands when your machine is going to sleep or waking up.



[ Reply to This | # ]
10.6: Fix sleep problems with SleepWatcher
Authored by: Andrew J Freyer on Dec 02, '10 10:34:48AM
Generally, I don't think its a good idea to recommend launchd modifications/additions without understanding exactly what they're doing. The reason is that if they cause issues they are difficult to debug for the average user. Especially in circumstances like this where a slight error could cause the computer to continually sleep or shut down.

That being said, although I have not tested this script either, this seems like a valid method of forcing an idle-time based sleep if other methods fail.

This could benefit from some explanation as to what the launchd file actually does. First, the option "KeepAlive" will perform the "ProgramArguments" whenever the process sleepwatcher is not running. Similarly "RunAtLoad" will perform the "ProgramArguments" at login.

As for the actual program arguments, they are a bit more complex:

 
<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>

The first line calls an instance of sleepwatcher with three arguments: -V (for verbose logging), -S (to run a command on display sleep), -W (to run a command on display wake)

The -S command will run the command

/usr/local/sbin/sleepwatcher -d -t9000 -i "/usr/local/sbin/sleepwatcher -n" -p /tmp/sleepwatcherid
every time the display sleeps. This command launches ANOTHER process of sleepwatcher which is a daemon (-d) that waits for 900 seconds (-t9000) of idle activity (-i) before it performs the command
/usr/local/sbin/sleepwatcher -n
while writing ITS OWN (read: second instance of sleepwatcher) process identifier PID to the file /tmp/sleepwatcherid.

Now, the idle command

/usr/local/sbin/sleepwatcher -n
launches a THIRD instance of sleepwatcher with the single argument -n that tells the THIRD instance of sleepwatcher to cause the computer to sleep and immediately quits the THIRD INSTANCE.

Lastly, the -W command will kill the SECOND instance of sleepwatcher, leaving the first instance running.

Finally, I might note that this is very easily removed from launchd with the same effect. By adding a "-d" tag to the FIRST instance (with the four arguments), and calling the entire command from AppleScript, the result will be the same (so long as the script is saved as an application and is added to the login apps).

Always make sure that you understand what a script is going to.

---
Manage Mac OSX settings depending on your location - AirPort Location for Mac



[ Reply to This | # ]
10.6: Fix sleep problems with SleepWatcher
Authored by: zyuvi00 on Dec 03, '10 04:16:36PM

So I decided to follow through on Andrew's suggestion and create a user-level application (via Automator in my case since I'm not too script savvy). If anyone is interested, here is what I did:

1. Create an Automator "Application"

2. Add a "Run Shell Script" action to the workflow.

3. Paste the following text into the shell script window:
/usr/local/sbin/sleepwatcher -d -S '/usr/local/sbin/sleepwatcher -d -t6000 -i "/usr/local/sbin/sleepwatcher -n" -p /tmp/sleepwatcherid' -W 'kill -9 `cat /tmp/sleepwatcherid`'

4. Replace "6000" with however long you want it to wait. (Value is in 0.1 seconds so 6000 = 10 minutes.)

5. Save the application.

6. Go to "Systems Preferences > Accounts" and add this application to your Login items

Since I'm new to the Mac I was nervous about this doing things like continually launching more and more processes without killing them properly or something like that, so I tested it by setting the sleep time to 10 seconds (-t100) and running it in a bunch of scenarios while watching the Activity Monitor... Seems to work for me.

Thanks for all the pointers in this discussion thread -- helped me work around a frustrating problem with my system crashing every time the OS tried to put my system (with a newly installed SSD) to sleep via the Energy Saver Prefs Pane...



[ Reply to This | # ]
10.6: Fix sleep problems with SleepWatcher
Authored by: agitatedString on Oct 30, '11 12:59:12AM

Hackintoshes? Anyway, getting sleepwatcher to work for me was as simple as putting the following into my ~/.bash_profile file:

result=$(lsof | grep -i sleepwatcher)
if [ -z "$result" ]; then
/opt/local/sbin/sleepwatcher --daemon --sleep /Users/pcs/bin/sleepscript.sh --wakeup /Users/pcs/bin/wakescript.sh
fi



[ Reply to This | # ]
10.6: Fix sleep problems with SleepWatcher
Authored by: shinew7911 on Dec 02, '10 12:20:08PM
asmeurer, yes it is. Homebrew is just a personal preference of mine and it is easiest one to setup in my experience.

fds, the point of the hint is to help people work around their auto-sleep problem, regardless of the hardware used. And no SleepWatcher is not "just a tool to run your own commands when your machine is going to sleep or waking up.", and no it would not address the issue by "simply having SleepWatcher installed would fix". Feel free to read up the man page linked below to understand what it does.

Andrew, thanks for the detailed explanations. Yes I should've described in more details what each of the option does. This is my first hint and I didn't want to make it longer than it already was, noob mistake :/
But I would also like to add that the advantage of putting the plist in "/Library/LaunchDaemons/" is that once configured, it works on the root level across multiple accounts and it's pretty much maintenance free. Although I strongly recommend copy & paste the code to avoid any typo.

To provide more clarity to those of you who would like to find out more about all the available options for SleepWatcher without installing it, here is its MAN page.

[ Reply to This | # ]
10.6: Fix sleep problems with SleepWatcher
Authored by: allanmarcus on Dec 02, '10 01:48:36PM

Or just use narcolepsy:

http://webpages.charter.net/remsoftware/util-scripts.html#narc



[ Reply to This | # ]
10.6: Fix sleep problems with SleepWatcher
Authored by: SleePy on Dec 02, '10 04:01:19PM

I had a sleep issue with my macs running 10.5. I gave in after some time and just reinstalled it. Sleep now works just fine.



[ Reply to This | # ]
PleaseSleep
Authored by: derekcurrie on Dec 08, '10 07:04:13PM

A free utility I've been using for years for this situation is PleaseSleep.

http://www.dragonone.com/products/macosx/pleasesleep/

It is customizable so you can have it run in your menubar or as a floating panel. You can enable logging and choose what to log. You can have it force sleep only when particular apps are running. You can also set it up to NOT force sleep when particular apps are running, such as your backup apps, repair apps, video editing apps, anything you wish. You can also set it up to force sleep anyway at a particular time of the day.

It's donationware. It works flawlessly.



[ Reply to This | # ]
10.6: Fix sleep problems with SleepWatcher
Authored by: hickscorp on Nov 30, '11 04:29:42PM
Hello, i have been using this hack for a lot of time now, and i just realized that my .sleep / .wakeup scripts could help some of you. My sleep script takes care of two issues i had with my Mac (Even with Lion). - A lot of my peripherals are connected to my computer via Bluetooth. When my Mac goes to sleep, i want to avoid it being woke up by my cat playing with the mouse or walking on the keyboard. That's why i am using blueutil tools in conjunction with the tools described here. However, i don't want my bluetooth peripherals to be connected upon wake if they were not connected before sleeping. That's why my script writes the status of bluetooth radio in a file (.oldBlueStatus) before sleeping, so it is able to know if it has to re-activate it upon waking up or not. - If you have a software RAID, ejecting the volumes mounted in /Volumes isn't sufficient to fully eject the physical disks on which the strips / mirrors are located. Therefore, i use the appleRAID command to take care of that. You may notice that i'm testing for "/Volumes/Data" before ejecting the volumes. This is specific to my set up: in my Macbook Pro i have a SSD for my system, and a mechanical HDD for my data. My whole home sits on the mechanical, so it has to stay mounted whatever happens, hence the test on "/Volumes/Data". Below are the scripts i wrote to take care of the jobs. .sleep #!/bin/bash DF="/bin/df" CAT="/bin/cat" LG="/usr/bin/logger" AWK="/usr/bin/awk" CUT="/usr/bin/cut" TR="/usr/bin/tr" DU="/usr/sbin/diskutil" BU="/usr/local/bin/blueutil" # Growls one argument. function growl { osascript <<-EOF tell application id "com.Growl.GrowlHelperApp" to notify with name "SleepWatcherEvent" application name "Sleep Watcher" title "Sleep Watcher" description "&#36;1" EOF } # Detach from any removable media. This would also work: # osascript -e 'tell application "Finder" to eject the disks' for vol in `&#36;DF | &#36;AWK '{print &#36;1":"&#36;6}'`; do mp=&#36;{vol##*:} if [ "&#36;{mp%/*}" == "/Volumes" ] &amp;&amp; [ "&#36;mp" != "/Volumes/Data" ]; then &#36;LG "Detaching from &#36;{vol##*:}" echo &#36;{vol##*:} &#36;mp hdiutil detach &#36;{vol##*:} 2>/dev/null; fi done # Detach from any RAID container set. for vol in `$DU appleRAID list | grep disk | $CUT -d\ -f3 | $CUT -c1-5 | sort | uniq`; do $LG "Detaching from $vol" hdiutil detach $vol > /dev/null 2>/dev/null; done # Disable BlueTooth. OldBlueStatus=`$BU status | $CUT -d\ -f2 | $TR -d "\n"` echo $OldBlueStatus > .oldBlueStatus if [ "$OldBlueStatus" = "on" ]; then $LG "Disabling Bluetooth..." $BU off fi .wakeup: #!/bin/bash DF="/bin/df" CAT="/bin/cat" LG="/usr/bin/logger" AWK="/usr/bin/awk" CUT="/usr/bin/cut" TR="/usr/bin/tr" DU="/usr/sbin/diskutil" BU="/usr/local/bin/blueutil" # Growls one argument. function growl { osascript <<-EOF tell application id "com.Growl.GrowlHelperApp" to notify with name "SleepWatcherEvent" application name "Sleep Watcher" title "Sleep Watcher" description "&#36;1" EOF } OldBlueStatus=`&#36;CAT .oldBlueStatus | &#36;TR -d "&#92;n"` if [ "&#36;OldBlueStatus" = "on" ]; then &#36;LG "Enabling Bluetooth..." &#36;BU on fi</code> i hope this will help. Pierre. P.S.: Sorry for the bad english. It's late here.

[ Reply to This | # ]