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

Cure an insomniac Mac with SleepWatcher System
Since 10.3.9 or thereabouts, my Mac has been unable to put itself into a deep sleep no matter what I set in the Energy Saver preferences panel -- the monitors will turn off, but the fans don't spin down. This seems to be a common problem, according to the DssW Sleep Centre and Apple's own forums. I am able to put it to sleep using the apple menu item, or by pressing Ctrl-Eject then the S key.

I have now found a solution to my woes, in the form of SleepWatcher from Bernhardt Baehr, which is licensed under the GNU GPL. To make the magic happen, you download and install both the daemon itself and the StartupItem as described in the included instructions. Read the man page for SleepWatcher so you get a feel for its capabilities.

By default, SleepWatcher runs in daemon mode, and executes /etc/rc.wakeup and /etc/rc.sleep just after your Mac wakes, or just before it sleeps. These scripts look for currently logged-in users and execute scripts called ~/.wakeup and ~/.sleep if they exist. So far so good. Now to add the auto-sleep after my preferred idle time. The StartupItem is stored in /Library -> StartupItems -> SleepWatcher -> SleepWatcher, and this file sets the options for the daemon when that starts:
StartService ()
{
    ConsoleMessage "Starting Sleep Watcher"
    /usr/local/sbin/sleepwatcher -d -V -s /etc/rc.sleep -w /etc/rc.wakeup
    /etc/rc.wakeup
    return 0
}
So the simple change is to edit this file (you need your Administrator password) to include the -t (timeout) and -i (idle command) options. Usefully, SleepWatcher can call itself, and it has a 'sleep now' option, -n. Note that the -t option is given in tenths of a second, so as I like to sleep after 20 minutes, I set this to 12000. My version looks like this (the user/local/sbin... line has been split in two for a narrower display here; enter it as one long line):
StartService ()
{
    ConsoleMessage "Starting Sleep Watcher"
    /usr/local/sbin/sleepwatcher -d -V -s /etc/rc.sleep -w /etc/rc.wakeup
      -t 12000 -i "/usr/local/sbin/sleepwatcher -n"
    /etc/rc.wakeup
    return 0
}
Other ways of using SleepWatcher could include setting the -f (config file) option to point to a separate location in the StartupItem, then you can add commands to the config file rather than editing the StartupItem to make changes. Or you might arrange to run sleepwatcher commands from your login account to set things up differently for various users. Note that the last logged-in user to set options wins in this case, as there is only one daemon running for the whole system!

[robg adds: SleepWatcher has been covered here before a few times, being put to different uses.]
    •    
  • Currently 3.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[21,382 views]  

Cure an insomniac Mac with SleepWatcher | 7 comments | Create New Account
Click here to return to the 'Cure an insomniac Mac with SleepWatcher' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Cure an insomniac Mac with SleepWatcher
Authored by: timrand on Feb 28, '06 03:02:50PM

I had a similar problem, but I found that I had a USB device that would not accept the power-down request. I unplugged that USB device and all was well.



[ Reply to This | # ]
Cure an insomniac Mac with SleepWatcher
Authored by: aqsalter on Feb 28, '06 11:37:17PM

You can also Sleep a Mac immediately by pressing Cmd-Opt-Eject.
Ctl-Eject brings up the "Logout, Sleep, Shutdown" prompt.

Be careful because (from memory) Cmd-Ctrl-Eject shuts down the computer - with no prompts for confirmation.



[ Reply to This | # ]
Cure an insomniac Mac with SleepWatcher
Authored by: zmagyar on Mar 01, '06 05:48:05AM

In my case it was the internet sharing which prevented the mac from going asleep. Once off, all works fine, just FYI
Zsolt



[ Reply to This | # ]
SleepWatcher doesn't recognise printing
Authored by: postglock on May 18, '06 03:49:06AM
Great hint, been having this problem with my girlfriend's eMac, which just didn't want to sleep on idle. Tried unplugging alll USB devices, making sure sharing was off, creating a new user, everything!

The only problem is that SleepWatcher will force sleep when printing. Just wondering if anyone had a tip to stop this. My current workaround is to run Jiggler when printing. This works okay, if a bit clunky, although it would be even better if Jiggler allowed you to activate itself by process as opposed to application. Of course, if there was a way to make SleepWatcher recognise printing as a non-idle process it would be even better!

[ Reply to This | # ]

Cure an insomniac Mac with SleepWatcher
Authored by: magir on Sep 03, '06 12:13:08AM

My wifes iBook also had sleep problems. I tracked it down to Eudora and sometimes other Internet-related apps. If the application was running, the machine never slept automatically. With this hint I was able to solve the issue - hopefully we do not run into the described printing problems.



[ Reply to This | # ]
Setting idle perameters per user
Authored by: magnamous on Aug 15, '08 05:52:52PM

I managed to get this working, so I thought I'd share. I did this with SleepWatcher 2.0.5 installed:

  1. Using your favorite text editor as root, open
     /Library/StartupItems/SleepWatcher/SleepWatcher
    Modify this:
    
    StartService ()
    {
            ConsoleMessage "Starting Sleep Watcher"
            /usr/local/sbin/sleepwatcher -d -V -s /etc/rc.sleep -w /etc/rc.wakeup
            /etc/rc.wakeup
            return 0
    }
    
    so that it looks like this:
    
    StartService ()
    {
            ConsoleMessage "Starting Sleep Watcher"
            /usr/local/sbin/sleepwatcher -d -V -s /etc/rc.sleep -w /etc/rc.wakeup -t 18000 -i /etc/rc.idle
            /etc/rc.wakeup
            return 0
    }
    
    The only line that changes is /usr/local/...
    I used 18000 because I wanted it to do something after 30 minutes of idleness.

  2. Now type this in the Terminal as root:
    
    cp /etc/rc.sleep /etc/rc.idle
    
    and as before, open rc.idle in your text editor of choice. In there, change this:
    
    for user in `echo 'show State:/Users/ConsoleUser' | scutil | awk '/kCGSSessionUserNameKey/ { print $3 }'`; do
            home=`eval echo ~$user`
            if [ -x "$home/.sleep" ]; then
                    logger -t $0 "executing .sleep of user $user"
                    su - $user -c "$home/.sleep"
            fi
    done
    
    so that it looks like this:
    
    for user in `echo 'show State:/Users/ConsoleUser' | scutil | awk '/kCGSSessionUserNameKey/ { print $3 }'`; do
            home=`eval echo ~$user`
            if [ -x "$home/.idle" ]; then
                    logger -t $0 "executing .idle of user $user"
                    su - $user -c "$home/.idle"
            fi
    done
    
    In other words, replace all instances of .sleep with .idle.

  3. Now, in your home folder, create a new text file called .idle, and add whatever you want it to do when it's idle. (I added a line to tell it to fast-user-switch to the login screen.) Once you've saved your changes, remember to make the file executable by typing chmod +x .idle.

When I first created this, I wanted to test it to make sure that it worked, so I set -t in the first step to 10 instead of 18000 because I was impatient. Do not be impatient. If you want to test this to make sure it works, make -t at least 100 or 200 (10 or 20 seconds) to give yourself some breathing room for changing it back once you've discovered that it works.

The nice part about this implementation is that each user can specify what to do at idle time, instead of one implementation to rule them all (so to speak). Even nicer would be if each user could specify what he wants the delay to be before the .idle script kicks in. However, I have no idea how to implement that. Perhaps by using the --config option...? If someone knows how, by all means, please reply.

[ Reply to This | # ]

Cure an insomniac Mac with SleepWatcher
Authored by: toxi on Jan 05, '10 02:27:49AM

Hello,

I have the problem with my mac for more than two year, impossible to have it sleep. I change my mac, reimport all data, but the problem is still there.

Would Sleepwatcher work with 10.6?



[ Reply to This | # ]