9

I sometimes have the problem that my macbook won't sleep, even with the lid closed. After some investigation I tracked the problem down to Spotify app; when the app is open, even if no song is playing, my macbook won't go to sleep and eventually go out of battery power. I don't have need of spotify keeping my mac awake (for obvious reason I'm ok with a video player or a game instead); is there a way to ignore its request for preventing sleep without affecting other applications? I know I could quit spotify when not using it, but I am prone to forget it and it's very annoying to find my mac completely drained...

EDIT: to be clearer, I am not looking for a solution about Spotify specifically (I could simply uninstall it and forget about it), I am just looking for an option which is the default in both Linux and Windows, that is that the user always overrides any setting when he/she closes the lid.

3 Answers3

2

To manually initiate a sleep on the mac and override 'Preventing Sleep' applications run

pmset sleepnow

in the terminal

Edit

I agree, the Apple spec is flawed, Apple need to fix this!!!, there should definitely be a way to override the 'prevent sleep' system wide, however the above is the best workaround I've found...

...and if you want something that does a bit more checking (some apps I've discovered still prevent sleeping with the above command)

function slp2 {
  #add a list of programs here that you will always wish to close
  pkill -x qemu-system-i386
  pkill -x qemu-system-x86_64
  sleep 2
  assertion=$(pmset -g assertions | egrep 'PreventUserIdleSystemSleep');
  canSleep=${assertion:34:1};
  if [ $canSleep != "0" ]; then
    say "cannot sleep, please close offending applications"
  else
    pmset displaysleepnow
    say "goodnight, rest well"
  fi
}

You can also assign this to a Keyboard Maestro command so you don't need to open up a terminal and type in. Keyboard Maestro has an 'At system sleep' trigger (possibly there is some other commands, lid close maybe!) that may help further.

Edit2 Updated my Mac to Sonoma (14) and my mac again stays on all night or drains the battery! (come on Apple, save some carbon emissions!) I think it is because I have bluetooth set to on. So, I installed sleepwatcher (run commands when we sleep / wake the computer) & blueutil (allows us to switch on and off bluetooth)

brew install sleepwatcher
brew services start sleepwatcher
brew install blueutil

Now, create a .wakeup file and a .sleep file in your /Users/yourusername directory.

.sleep

#!/bin/bash
/usr/local/bin/blueutil --power 0

.wakeup

#!/bin/bash
/usr/local/bin/blueutil --power 1
atreeon
  • 476
  • Thank you, this works! Now I can force my Mac to go sleep by pressing a custom keyboard shortcut. – Jonny Aug 26 '23 at 02:26
1

Your MacBook should always sleep when the lid is closed. If your Mac is not sleeping with the lid closed, this suggests a problem.

There are exceptions:

  • Some MacBooks can be plugged into external displays and, once connected, the MacBook's lid can be safely closed without triggering system wide sleep.

  • Previously it has been possible to install a third party kernel extension to override macOS's behaviour and stop sleep with the lid closed. Doing this carries risk and should be avoided.

Blocking Sleep

It is difficult for user space processes to block sleep. Sleep assertions should never block a user initiated sleep, either via closing the lid or requesting sleep from the Finder. Use pmset to view current power assertions:

pmset -g assertions
Graham Miln
  • 43,776
  • I know the problem with external displays and it's not my case; also I didn't install anything which tampered with energy management (I know tools like Caffeine, but I simply don't need them on my macbook). When the lid is closed the computer is usually off, but I have experienced once or twice when this was not the case: the "prevent sleep" settings acted above it – Riccardo Cossu Feb 09 '17 at 10:26
  • 1
    The behaviour you are seeing suggests a lower level bug in macOS, a problem with connected hardware, or a bug in the hardware of the Mac. This assumes there are no third party kernel extensions installed. – Graham Miln Feb 09 '17 at 10:28
  • 1
    I think I will go the easy way and throw spotify away :-) I will also try to understand if something is wrong with my macbook – Riccardo Cossu Feb 09 '17 at 10:45
1

I sometimes have this issue with Spotify. An application can block sleeping even when laptop's lid is closed, ie by calling:

[[NSProcessInfo processInfo] beginActivityWithOptions:NSActivityUserInitiated reason:@"Good Reason"];

I suppose we shoudn't do that to prevent AppNap but the documentation is not very clear.

grg
  • 201,078
matthieu
  • 111