Mac OSX Lion not sleeping

While using a brand new 2011 iMac (21″), I noticed that putting it to sleep (Apple Menu > Sleep) would not work – the screen would turn off, but the computer itself would still remain on.

Here’s the solution – including an application which you can use!

While using a brand new 2011 iMac (21″), I noticed that putting it to sleep (Apple Menu > Sleep) would not work – the screen would turn off, but the computer itself would still remain on.

I decided to do a bit of research and found that running the command pmset -g assertions from the terminal would list any conditions currently active which are preventing the OS from sleeping. In my case, it was file sharing as well as remote TTY sessions (via SSH), which were solved by issuing the command sudo pmset -a ttyskeepawake 0.

Now, Apple should really be informing users if there are any issues preventing a computer from sleeping. It makes it even more annoying since the iMac has no status LEDs, so it’s almost impossible to tell if it’s gone to sleep, except by listening very carefully for the fans/HDD spinning down.

I decided to write a very quick application, which, when launched, tries to sleep the computer, and if there are any conditions preventing it from doing so, it will list them out.

You can download it here along with the source code (which is messy and perhaps buggy, since I did it very fast). For me, it works perfectly. I can’t claim any responsibility for running it, although I don’t see how it could cause any issues.

(I also found another application here which seems like a better/longer term solution, although I have not tried it out myself and so claim no responsibility. You may want to try it instead, if you’re interested!)

Sleep Checker.dmg, 98KB
(SOURCE CODE)
Requires OSX 10.7 Lion and Python 2.7 (Already installed by default on Lion)

Screenshots

If there are conditions preventing sleep:

Once it’s ready to sleep:

Apple, please fix this by letting users know if there are conditions preventing system sleep, and providing a “force sleep” option, perhaps?

Interpreting pmset

If you’re interested in what’s actually causing the problem, here’s a quick rundown on the output of pmset -g assertions:

$ pmset -g assertions
9/30/11 2:48:44 PM GMT+ 
Assertion status system-wide:
   ChargeInhibit                           0
   PreventUserIdleDisplaySleep             0
   PreventUserIdleSystemSleep              1
   NoRealPowerSources_debug                0
   CPUBoundAssertion                       0
   EnableIdleSleep                         1
   PreventSystemSleep                      1
   DisableInflow                           0
   DisableLowPowerBatteryWarnings          0
   ExternalMedia                           0

Listed by owning process:
  pid 42096: [0x0000012c0000a470] PreventSystemSleep named: "com.apple.AppleFileServer" 
  pid 19: [0x0000012c00000013] PreventUserIdleSystemSleep named: "com.apple.powermanagement.ttyassertion" 
        Details: /dev/ttys000
        Localized=A remote user is connected. That prevents system sleep.

...

You can see that there are 2 conditions which are preventing sleep in this case (PreventSystemSleep and PreventUserIdleSystemSleep). One of them is a process (basically, like a program/application) which has the ID 42096 and is called “com.apple.AppleFileServer”. This happens to be file sharing. The other process (PID19) happens to be a condition which is asserted when remote users are connected via SSH.

You have to manually terminate these processes or remove their assertions, but do be careful when doing so because not all processes can be killed neatly – you may end up crashing your system if you’re not careful!

For more information on pmset, you can issue the command “man pmset” to get help on how to use it.