The native caffeinate
utility is not working to prevent my macbook from sleeping when I close the display. Instead, the system sleeps and various network services ( VPN, screenshares, VOIP calls, messaging clients, file transfers ) to fail while I walk from my desk to my meeting in < 5 minutes.
Walking around with the laptop lid half open makes it difficult to safely carry with my coffee, and is distinctly uncool.
The native caffeinate
utility is designed to handle this as a user-friendly replacement for pmset
, but appears to be designed not to work when disconnected from AC power.
Docs via man caffeinate
-s Create an assertion to prevent the system from sleeping. This assertion is valid only when system is running on AC power.
Looking for a native alternative to InsomniaX or nosleep.
A timeout feature is essential so that I avoid accidentally stuffing my undead macbook into a bag, resulting in a hot mess. ( caffeinate -t
, specifies the timeout value in seconds )
Best Answer
It's not ideal, but here's a solution. To prevent the laptop from sleeping when the lid is closed and you're running on battery, run the following commands:
To re-enable laptop sleeping when the lid is closed and you're running on battery, run the following commands:
The "5" in the second set of commands represents the number of minutes before sleeping when on battery; adjust as desired for your laptop.
This is a bit dangerous, since if you forget to re-enable your settings, the laptop will never sleep when on battery. Because of this, I've written a shell script to automatically re-enable the settings:
The shell script will disable sleeping until you hit the Enter key, at which point it will re-enable the sleep settings (alternately, you can hit Ctrl-C and achieve the same thing). It will also set a timeout (defaults to 300 seconds/5 minutes) after which the sleep settings will automatically be re-enabled, and the laptop will be forced to go to sleep. While this would be a pain if you're using your laptop in a meeting, it will be a lifesaver if you forgot and put your laptop in your bag to go home.
Astute readers will note that these commands require
sudo
; sadly, that's unavoidable AFAIK. What I've done on my system is to make it so that I don't have to enter my password to runpmset
as root. To do that, edit thesudoers
file (sudo visudo
) and add this line:replacing "joe" with your username. You could probably achieve the same result (i.e. running the script without having to enter your password) by running the shell script SETUID, but I don't like doing that; opening up this one command via sudoers seems less risky to me.
To run the script, stick it in a directory on your
PATH
and invoke it as such:When you get to where you're going, simply hit Enter or Ctrl-C and you're good to go. And if you forget about it, it will automatically reset and sleep.
There's probably a way to achieve all of this via AppleScript, so that you can then assign it a hot key and what not; I'll try that if I ever get tired of running this from the command line.