Spotify Prevents MacOS to Sleep
Having some time these days – with holidays and stuff – decided to see why my Macbook Pro does not sleep when I close the lid.
The short explanation is that after you open Spotify and play a song, the program will prevent your system to sleep. It’s a very known old bug from 8 years ago now.
There is a relatively quick fix for that:
-
- Install Homebrew – a package manager for Mac:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
- Install sleepwatcher – it monitors sleep wakeup and idleness of a Mac:
brew install sleepwatcher
- Create a
.sleep
file for sleepwatcher to execute when we close the lid. Run the command:echo "osascript -e 'tell application "\"Spotify"\" to quit'" >> ~/.sleep && chmod +x ~/.sleep
- (optional) if you want Spotify to resume playing at system wake-up, run this:
echo "osascript -e 'tell application "\"Spotify"\" to play'" >> ~/.wakeup && chmod +x ~/.wakeup
- Run it as a service. If you don’t have enabled homebrew services, you must run this command
brew tap homebrew/services
and then run sleepwatcher as a service:brew services start sleepwatcher
- Install Homebrew – a package manager for Mac:
That’s all. Now, you can test it by having the Spotify MacOS app running, then close the lid and wait few seconds until the system sleep. Re-open the lid and you should have your Spotify app closed.
Tested on macOS Catalina 10.15.2.
Waiting for your feedback! 🙂
Hi Silviu,
Thank you so much for this article! My Mac not sleeping properly has been bugging for ages and your solution is brilliant. My monitor says a huge thank you :).
By the way, step 4 appears to be missing “echo” at the start of the line.
As an aside, I found running `pmset -g assertions | grep PreventUserIdleSystemSleep` helpful for diagnosing what is preventing sleep in general. You may well have come across this and may not be worth including in your most excellent article.
Thanks again.
Hi, Grum,
I’m glad that the solution works for you.
Regarding step 4, the “echo” exists, but it’s on the previous line.
Thanks
anyway ‘brew services start sleepwatcher’ can be run at startup? so you don’t have to remember to do it?
You need to run it one time only and will run as a service.
Dear Silviu,
I installed everything and it works fine – however, how do I uninstall – or disable the resuming of spotify after waking up? Is there a command line for that as well?
Best,
Jack
Sorry for the late response, Jack.
You can delete the .wakeup file. The command for that would be:
rm ~/.wakeup
i tried to delete the wakeup file but it says:
.wakeup: No such file or directory
it has been working fine but i just want to remove the auto play on wake, the sleep is sufficient.
Error: Permission denied @ rb_sysopen – /Users/username/Library/LaunchAgents/homebrew.mxcl.sleepwatcher.plist
You can try this: https://stackoverflow.com/a/66641140/5389833