64

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 )

5
  • 1
    I fear there is no native alternative, as a MacBook is supposed to go to sleep when you close it. :-/
    – Asmus
    Commented Dec 16, 2015 at 23:31
  • 3
    Walking around with MacBook is exactly why I also want a solution for preventing sleep when lid is closed.
    – user150109
    Commented Sep 19, 2017 at 15:06
  • Did you try the option -t, like caffeinate -t 3600? According to my experience, it works even if the macbook is running on battery.
    – Qiang Xu
    Commented Mar 1, 2018 at 17:04
  • 1
    Thanks for suggestion, didn't work on initial attempts. Am I missing something? Tested 1) caffeinate -t 3600 2) Close laptop 3) Wait 5s 4) Open laptop to find login screen and disconnected network services.
    – here
    Commented Mar 1, 2018 at 19:09
  • @here I didn't try to close the lid, though. I just ran the command with -t option when my macbook is on battery, and it didn't go to sleep. I haven't tried with the lid closed, but it looks the macbook will be forced to sleep as soon as you close the lid. Sorry for my misunderstanding. I was thinking you were looking for a way to make it work when it runs on battery only, without the lid closed.
    – Qiang Xu
    Commented Mar 1, 2018 at 19:33

5 Answers 5

58

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:

sudo pmset -b sleep 0; sudo pmset -b disablesleep 1

To re-enable laptop sleeping when the lid is closed and you're running on battery, run the following commands:

sudo pmset -b sleep 5; sudo pmset -b disablesleep 0

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:

#!/bin/bash
#***************************************************************************
#*** noz - prevent laptop from sleeping when lid is closed
#***************************************************************************

#***** set some defaults *****
BATTERY_SLEEP=5 # in minutes
DEF_WAKE_LEN=300 # in seconds

#***** determine timeout value *****
timeout_len=${1:-$DEF_WAKE_LEN}

function prevent_sleep() {
    echo
    echo -n "Preventing sleep for $timeout_len seconds; press <enter> to continue..."

    sudo pmset -b disablesleep 1
    sudo pmset -b sleep 0
}

function enable_sleep() {
    # $1: <enter> = 0, timeout = 1, Ctrl-C = undef

    #----- insert a newline for timeout or Ctrl-C -----
    if [[ ${1:-1} -eq 1 ]]; then    echo; fi
    echo "Restoring previous battery sleep setting: $BATTERY_SLEEP"

    sudo pmset -b disablesleep 0
    sudo pmset -b sleep $BATTERY_SLEEP

    #----- sleep on timeout only -----
    if [[ ${1:--1} -eq 1 ]]; then   sudo pmset sleepnow; fi
    exit
}

#***** prevent it from sleeping *****
prevent_sleep

#***** trap Ctrl-C *****
trap enable_sleep INT

#***** wait for an enter *****
read -t $timeout_len
rc=$?

#***** re-enable normal sleep *****
enable_sleep $rc

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 run pmset as root. To do that, edit the sudoers file (sudo visudo) and add this line:

joe ALL=(ALL) NOPASSWD: /usr/bin/pmset

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:

noz [<timeout in seconds>]

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.

3
  • 9
    Cool! Thanks a lot! A suggestion / simplification: I don't think you need to change the sleep setting. Just switch disablesleep on and off. pmset -a disablesleep 1 and pmset -a disablesleep 0 work fine for me. My MacBook doesn't got to sleep when I close the lid. All other settings remain the same. Commented Nov 1, 2017 at 2:17
  • Github user iccir has made a free app called Fermata that uses this trick under the hood and wraps it up nicely with a menubar icon and a timer, etc. I just tried it on Mojave (10.14.2) and it worked great for me. github.com/iccir/Fermata
    – Alex Ryan
    Commented Dec 31, 2018 at 8:08
  • 2
    Maybe worth mentioning that sudo pmset -b disablesleep 1 will persist the state across reboots, and that the Sleep option under the Apple logo menu () will stay disabled until you re-enable sleep. You must run sudo pmset -a disablesleep 0 to re-enable sleep.
    – ccpizza
    Commented Jan 28, 2020 at 8:07
10

There's no native way to do it. Here is what I got after some searching and trials:

enter image description here

Note:

  • Nosleep's kernel extension failed to be loaded on High Sierra
  • The private SPI is NOT supported by Apple after High Sierra. Fermata switched to the pmset method after version 1.1
  • You may need to disable Gatekeeper to let Insomnia work as expected.
  • The other apps I tried don't work with lid closed
1
  • 1
    There's one hacky native way I know how to do this: Start a screen sharing session with the target Mac and it won't sleep even if the lid is closed and battery power is removed. I'm referring to the built-in macOS screen sharing functionality and not a 3rd party's. When a session is active thescreensharingd process on the target mac takes out a the power assertionPreventSystemSleep and this is actually respected, preventing system sleep.
    – Chris
    Commented Feb 12, 2022 at 4:08
8

Github user iccir has made a very handy little free menubar app called Fermata that does exactly what you want: keeps a MacBook awake with the lid closed, and allows you to set a timeout duration.

I just tried it on Mojave (10.14.2) and it worked great for me. https://github.com/iccir/Fermata

8

tested on macOS Catalina Version 10.15.6

works both on battery mode and charger mode.

sudo pmset disablesleep 1

do not forget to restore settings by

sudo pmset disablesleep 0

My daughter often close the lid once she found I left my seat while I am still listening YouTube, this pmset command helps me to continue listening YouTube.

0
2

Most of the answers here include a bunch of manual work, so I made a tool that does things automatically. While it's been made because I want to prevent sleeping while I'm ssh'd into the mac, it can also be used to prevent sleep in other scenarios. It works when the lid is closed, doesn't get confused when there are multiple instances running, and automatically restores normal settings when the last instance closes. It's a script so it's fully hackable and it's well documented.

If you just want to prevent sleep when ssh'ing into the mac, you can set this up once and then forget it exists.

https://github.com/cheater/pmset-session/

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .