6

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

12

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.

2
  • 6
    What is the purpose of sudo pmset -b sleep 0 and sudo pmset -b sleep 5 before the sleep disabling/enabling commands? 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
    Feb 11, 2022 at 0:34
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? 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
    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. 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

6
  • 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
    Jul 26 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. Jul 27 at 5:30
  • 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
    Jul 27 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. Jul 27 at 5:59
  • pmset -g | grep SleepDisabled doesn't return anything on my MBA?
    – nohillside
    Aug 1 at 9:10

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