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.

46 thoughts on “Mac OSX Lion not sleeping”

  1. hi,
    I used your app, it worked and returned a message: Listed by owning process:
    pid 434: [0x0000012c000001b2] PreventSystemSleep named: “org.cups.cupsd”

    So now I believe the problem lies in pid 434. But what is this? how can i fix it?
    Thanks

    Jeff

  2. THANK YOU!
    THANK YOU!
    THANK YOU!

    To help combat a Microsoft Office 2011 problem I had to set up a dummy localhost printer and made this default printer for my system.
    MS Word 2011 had a problem (which got fixed) where it polled the default printer constantly.
    Network attached printers confused it and Word would lock up OS X.

    The biggest problem with setting the default printer to localhost is that often I’d click the print icon and forget to change to printer to the real printer, so the document gets sent to the localhost printer.

    In Snow Leopard there was no problem, but since Lion was installed a document in the localhost print queue caused cupsd to set the PreventSystemSleep flag on.

    Apple (and I) thought this was a hard disk problem since at the same time I upgraded my hdd. Apple wanted to run major hardware level testing on my poor MacBook Pro and would keep it for a week!

    Because this was NOT a hardware problem Apple would have never found a cure for my problem.

    So… once again, THANK YOU! I never could have diagnosed and FIXED the problem without your utility!

  3. Awesome! This is really a major issue for lion, and I totally agree it is definitely apples responsibility to fix this and quickly, however I am super grateful you took the time to make and share this brilliant app. Kudos

  4. Hello,

    Your app isn’t working for me.

    Let me state my issue with sleep first: all versions of Lion (from 10.7.0 – 10.7.2) do not fall asleep as specified in Energy Saver preferences on my MBP (mid-2009). Closing the lid triggers sleep, and forcing sleep from the Apple menu works as well.

    Running pmset -g assertions lists only a “EnableIdleSleep 1” among all the other assertions. Every “Prevent…” has a 0 beside it.

    Here is the console output from running your app. I ran it extracted to my Desktop and with the DMG, and both produced the same error.

    11-11-15 12:45:07.153 PM [0x0-0x120120].org.pythonmac.unspecified.sleep: Traceback (most recent call last):

    11-11-15 12:45:07.153 PM [0x0-0x120120].org.pythonmac.unspecified.sleep: File “/Volumes/Sleep Check/Sleep Check.app/Contents/Resources/__boot__.py”, line 52, in

    11-11-15 12:45:07.153 PM [0x0-0x120120].org.pythonmac.unspecified.sleep: _run(‘/Users/nkw/dev/sleep.py’)

    11-11-15 12:45:07.153 PM [0x0-0x120120].org.pythonmac.unspecified.sleep: File “/Volumes/Sleep Check/Sleep Check.app/Contents/Resources/__boot__.py”, line 48, in _run

    11-11-15 12:45:07.153 PM [0x0-0x120120].org.pythonmac.unspecified.sleep: execfile(scriptpath, globals(), globals())

    11-11-15 12:45:07.153 PM [0x0-0x120120].org.pythonmac.unspecified.sleep: IOError: [Errno 2] No such file or directory: ‘/Users/nkw/dev/sleep.py’

    11-11-15 12:45:07.234 PM sleep: sleep Error

    11-11-15 12:45:09.951 PM com.apple.launchd.peruser.501: ([0x0-0x120120].org.pythonmac.unspecified.sleep[6980]) Exited with code: 255

    1. Sorry about the issues! I realized I accidentally uploaded a development version rather than a release version. Please try re-downloading it!

      However, if you’re saying that pmset -g assertions is returning a ‘0’ next to every ‘prevent…’, the app won’t help as that’s all it checks. It sounds like your mac is not falling asleep on idle? What is your timeout value in your energy saver prefs?

      Running “pmset -g” (without the ‘assertions’ part) should return the # of minutes set to fall asleep when idle (Look for the line ‘sleep’):

      $ pmset -g
      Active Profiles:
      AC Power -1*
      Currently in use:
      autorestart 0
      powerbutton 1
      halfdim 1
      panicrestart 157680000
      hibernatefile /var/vm/sleepimage
      networkoversleep 0
      disksleep 10
      sleep 30 < <<<< This should be the # of minutes on idle before sleeping
      hibernatemode 3
      ttyskeepawake 0
      displaysleep 10
      womp 1

  5. your app flagged the cupsd as the reason my computer hasnt properly gone to sleep for the last two months. checked the printers and sure enough, a printer I havent connected to since october has been holding me up. Thanks for the extremely helpful and simple app.

  6. Thank you so much for your contribution to the life quality of our family 🙂 After having cursed Lion for 4 months bc the IMac wouldn’t sleep, your application uncovered an old unused printer which was the cause of the trouble. Great work!
    Henrik

  7. What code do you append with pmset to interpet the PID that prevents sleep? Would you put the syntax so I can’ write it down? What a great little app..thank you.

    1. Hi, I’m not sure I entirely understand your question, basically, if I remember, pmset only supplies PIDs (numbers). You have to use other tools to figure out what the process is. In my case, I use “/bin/ps -p [pid] -o comm=”. Similarly you could use something like top or the activity monitor (which may be the easiest). There are other methods (i.e. going to the directory /proc/[pid]/ will give you lots of information – i.e there’s a symlink called ‘exe’ which points to the binary’s path) which may be easier or more complicated.

      That will give you the actual process which is preventing sleep. Unfortunately, you’ll have to figure that out manually – perhaps by googling the name of the process?

      Also, pmset may give you information like “com.apple.AppleFileServer” which one would just take to be file sharing.

  8. WOW, thank you so much!!!!!!!! I had actually given up on it, but got all worked up again today and found this!!!! what I did was run your app, got the “org.cups.cupsd” result and went to the printer preferences, where I found a printer listed, which I hadn’t plugged in for the las 6 months… it was more of a duplicate. I erased it. Didn’t turn off the “printer sharing” though and it’s sleeping now!

    THANK YOU!!!

  9. Hi!
    Your program actually helped me to find the reason for my Macbook Pro not to go to sleep anymore. After a quick search I figured that a print job was hanging in the queue. Now everything works fine again. Thanks a lot for your help. I was getting really annoyed.

    Regards!

  10. Thank you!

    Even though your program crashes consistently on my late 2008 MacBookPro, I was able to diagnose it’s insomnia with the information about pmset on this page.
    In my case it was also a printer that was listed in the printing preferences pane as “in use” even though I hadn’t connected to it since I don’t know when. Deleting it got rid of the process that was preventing sleep.

    best regards,

    Mark

  11. Hi, I’ve been battling with a sleep issue with my MacMini, although my issue is kind of the reverse of what you app does.

    My MacMini keeps going to sleep whilst I’m trying to stream content via iTunes Home Sharing, I was wondering if anyone reading this knows if the command “pmset -g assertions” shows any output for when iTunes Home Sharing is active.

    So far I can only see the ttyassersion when I’m logged in over ssh i see nothing for home sharing, problem is the MacMini is running headless so I can’t run the command from the unit, so it always shows the ssh pid.

    1. I’m slightly confused – if you want to keep it from sleeping, can you not change the energy saver settings under system preferences? Or is this something you want only when streaming? I’m not sure about this then, and what assertions home sharing may show (I don’t actually have a mac so I can’t easily check)

  12. I tried your program, and it simply asks me if I want to enter smart sleep mode. I have the same issue as Ray, Running pmset -g assertions lists only a “EnableIdleSleep 1” among all the other assertions. Every “Prevent…” has a 0 beside it.

    Ricks-MacBook-Pro:~ rwhinton$ pmset -g assertions
    4/21/12 1:27:52 PM CDT
    Assertion status system-wide:
    ChargeInhibit 0
    PreventUserIdleDisplaySleep 0
    PreventUserIdleSystemSleep 0
    NoRealPowerSources_debug 0
    CPUBoundAssertion 0
    EnableIdleSleep 1
    PreventSystemSleep 0
    DisableInflow 0
    DisableLowPowerBatteryWarnings 0
    ExternalMedia 0

    Without the “assertions”

    Ricks-MacBook-Pro:~ rwhinton$ pmset -g
    Active Profiles:
    Battery Power -1*
    AC Power -1
    Currently in use:
    standbydelay 4200
    standby 0
    halfdim 1
    sms 1
    panicrestart 157680000
    hibernatefile /var/vm/sleepimage
    disksleep 10
    sleep 2
    hibernatemode 3
    ttyskeepawake 0
    displaysleep 1
    acwake 0
    lidwake 1

    I looked at the printers, I only have one installed which is current and accurately setup. Nothing in the print queue. I deleted it anyway, and the sleep issue is persistent. When I close the lid, system still stays on. Selecting Sleep from the Apple menu clears the screen (black) but nothing will wake the system! Lion 10.7.3, late 2011 model MBP 13.3″ 2.8 Ghz, 4gb RAM 750Gb HD. This is driving me nuts!

    I set the sleep timer to 2, but the system never went to sleep. Screen went blank, but that was it.

    1. Yeah, unfortunately all my app checks is the Prevent… stuff, and if it’s not due to that, then I’m honestly not sure what to do. You could perhaps try disabling smart sleep, and try ‘normal’ sleep instead (“sudo pmset -a hibernatemode 0” in Terminal), but honestly, I’m pretty sure that won’t make a difference. Use “sudo pmset -a hibernatemode 3” to change it back.

      !!!!NOTE!!!! Do not simply follow any commands which start with ‘sudo’ (which gives the command full [root] system access) if you’re unsure what it does! Refer here for more details on hibernatemode/pmset: https://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man1/pmset.1.html AND/OR google “mac os disable smart sleep”

      And as I said, I don’t think it’ll solve your problem, but it’s about all I can think of? All the best!

  13. This might have solved part of my problem (macbook not sleeping with lid closed or sometimes when trying to force it to sleep via the apple menu) but it still won’t sleep with the lid open. Screen goes black but nothing else happens (no ‘breathing’ light, etc).

  14. Thank you!

    My Mac has had the sleep issue since updating to lion as well which i only really discovered when my time machine disk woke me up one night,

    it was the cups.cupsd issue on mine too, due to a printer i no longer use

    Apple should really be releasing an patch/update for it but your little application worked great 🙂

  15. When I run this command in the terminal it returns only one “preventidlesystemsleep” assertion… that is (and i just checked this), I can close my macbook pro, and it now successfully sleeps after having cleared an old print queue. But, I am not sure what the prevent… assertion i listed above refers to. Does this mean that it will not properly go to sleep if the screen is left open? It returned PID110 as the process attributed to the issue, which is under the “coreaudiod” process when i found it on my activity monitor, but I am afraid to just terminate it haphazardly. Does anyone know a. what exactly this process is responsible for b. how will the system respond if i terminate it, and c. is it going to fix the issue that I think it is?
    Thanks for any comments or thoughts.

    1. Hi Eric, “preventsystemsleep” assertions prevent your computer from sleeping, regardless of whether you manually select sleep from the apple menu. On the other hand, “preventidlesystemsleep” still allows you to manually sleep the computer, but the computer won’t go to sleep itself after the time specified in system preferences.

      Coreaudiod refers to some app playing sound/music. I.e. if your computer was normally set to sleep after 10 minutes idle, but you were playing music, it would stay awake.

      To remove the assertion, assuming you’re playing music, you should quit that app. You shouldn’t terminate coreaudiod itself.

  16. Hoong Ern, thanks so much for your little app. I too had a cupsd process preventing sleep, even though I had printer sharing turned off. I just deleted all the printers set up in the Print & Scan preference panel, except the two that I use regularly, and the problem was resolved. Brilliant!

  17. Hi! Thanks for the great information, everything else I’ve looked at hasn’t explained this so clearly.

    I now have the process that is keeping my macbook away. Running the command in terminal tells me this:

    Listed by owning process:
    pid 37: [0x00000025012c0001] PreventUserIdleSystemSleep named: “com.apple.metadata.mds”

    That is apparently the one keeping my computer awake. What is MDS? It tends to take up upwards of 50% of my CPU and keeps everything hot and awake!!! 🙁 what to do? Thank you so much in advance!!

    1. Wait, scratch that, the only reason I got that response in the Terminal was because my Spotlight was indexing at the time. That is done now, and the terminal command doesn’t return a single positive response except for enableidlesleep. So clearly there is something going on beyond that Terminal is seeing. Any ideas would be HUMUMGOUSLY appreciated!

  18. Oh, I was really hopeful this would work. I have intermittent problems with my Core I7 iMac not going to sleep. Sometimes it sleeps fine, other times it just never goes to sleep when I tell it to. I ran “pmset -g assertions” and got these in the result:

    PreventSystemSleep 1
    pid 537(smbd): [0x000000080000016e] 14:53:16 DenySystemSleep named: “com.apple.smbd”

    I imagine samba might be activated for communication with my VMWare Fusion Windows 7 VM that I have running most of the time. Nonetheless, sometimes it still sleeps fine with the VM running. Other times, like right now, it doesn’t, even when I’ve shut all the explorer windows and closed all applications in the VM.

    So, I was hoping Sleep Checker would help. But no, Sleep Checker tells me:
    “0 conditions are preventing system sleep/hibernation:”

    …even though clearly PreventSystemSleep is set to 1. I just upgraded to Mavericks, perhaps that’s a problem? (obviously Apple didn’t fix this issue in Mavericks…)
    If so, ahem, wanna update Sleep Checker for Mavericks? If you feel like it… 😉

    Also, FYI, your download link doesn’t work on Firefox; there, it just spits a bunch of binary-translated characters onto the screen. Never seen anything like that before. It does work just fine in Safari.

    Thanks!

Leave a Reply