7

I've used Caffeine before and it can stop my MacBook from sleeping when the lid is closed (sometimes I want to continue listening to something through headphones while moving around)

Is there a command that can be run from the terminal that will do the same thing?

I've tried caffeinate but it does nothing. InsomniaX does stop the MacBook from sleeping when the lid is closed, is there a way to implement this same functionality from the terminal though?

0

3 Answers 3

17

Disable Sleeping when the lid is closed running on battery

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

Re-enable sleeping when the lid is closed running on battery

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

Remember to re-enable or manually sleep your machine before putting it into a bag. It will get very hot very quickly in an enclosed space.

4
  • 6
    What is the purpose of sudo pmset -b sleep 0 and sudo pmset -b sleep 5 before the sleep disabling/enabling commands? Commented Apr 14, 2021 at 6:47
  • 1
    I can't find any mention of disablesleep in man pmset on macOS Monterey, why is that?
    – damd
    Commented Feb 11, 2022 at 0:34
  • @damd However, the command above still works. I was wondering the same thing, if there are other undocumented possibilities that I'm simply not aware of...
    – Alex
    Commented May 5 at 23:48
  • 1
    @GeekInDisguise This sets the number of minutes that the system is inactive, before it automatically goes to sleep. So in addition to disabling sleep due to the closing of the lid, Chris also disables sleep due to the inactivity timer. The other command enables it back after 5 minutes of inactivity.
    – Alex
    Commented May 5 at 23:50
0

No. Only setting up closed clamshell conditions will prevent sleep when the proximity sensor indicates the lid is closed.

Short of finding a way to modify the firmware or unload / set some hidden arguments to disable sleep, the most viable solution might be hoping that power nap “dark wakes@ allow you to get whatever actions done while “sleep” conditions are present.

3
  • How does Caffeine/InsomniaX do it?
    – user150109
    Commented Sep 19, 2017 at 10:13
  • @theonlygusti - InsomniaX doesn't seem to be working since El Cap. and newer and as far as I know Caffeine never worked with the lid (it also doesn't work now)
    – Allan
    Commented Sep 19, 2017 at 10:38
  • @Allan InsomniaX still works perfectly for me, I just tried after seeing this answer that claimed it worked. However, InsomniaX is not a perfect solution for me, I'd much rather have a way to do this from the command line without needing to install an entire application.
    – user150109
    Commented Sep 19, 2017 at 15:18
0

Instead of writing shell commands on terminal every time create a bash script.

#!/bin/bash
sleepvar="$(pmset -g | grep SleepDisabled)"
sleepval="$(grep -c 0 <<< $sleepvar)"
if [ "$sleepval" -eq 1 ];
    then 
        sudo pmset -b sleep 0; sudo pmset -b disablesleep 1
        #echo "SleepDisabled set to 0, setting to 1 i.e. Disabling sleep"
    else
        sudo pmset -b sleep 1; sudo pmset -b disablesleep 0
        #echo "SleepDisabled set to 1, setting to 0 i.e. Enabling sleep"
fi

save it as <filename>.command , give it execution permissions using sudo chmod +x <filename>.command

after execution, use cmd+q to close the terminal,

or to close automatically:

open terminal-> open "Terminal" menu -> Settings-> Shell -> open "when the shell exits" dropdown -> choose "close if the shell exited cleanly"

or you can alternatively create AppleScript Application for a more UI friendly experience, I have posted the steps here.

https://apple.stackexchange.com/a/462512/501809

9
  • 1
    That’s just an AppleScript wrapper for shell commands. You can put a shell script (or link to it) on your desktop and double click that the same way.
    – Allan
    Commented Jul 26, 2023 at 20:19
  • @Allan yes it is shell script , i tried what you are suggesting and after failing to run sudo commands, i later moved to Applescript wrapper. Applescript application makes it convenient to input your password. Commented Jul 27, 2023 at 5:30
  • 1
    sudo will stop execution and wait for a password. To make it seamless , you modify the sudoers to allow that command to run with no password. It’s accomplished with 1/10th the code.
    – Allan
    Commented Jul 27, 2023 at 5:34
  • @Allan Thanks for making me aware about sudoers file. Surely one can put the efforts in modifying the sudoers file and get rid of 3 extra lines to create Applescript wrapper around the shell script , although 1/10th of the code seems and exaggeration. Commented Jul 27, 2023 at 5:59
  • pmset -g | grep SleepDisabled doesn't return anything on my MBA?
    – nohillside
    Commented Aug 1, 2023 at 9:10