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?

minseong
  • 8,824

3 Answers3

15

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.

nohillside
  • 100,768
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.

bmike
  • 235,889
  • How does Caffeine/InsomniaX do it? – minseong Sep 19 '17 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 '17 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. – minseong Sep 19 '17 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

nohillside
  • 100,768
  • 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 '23 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. – Ankit Veer Singh Jul 27 '23 at 05: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 '23 at 05: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. – Ankit Veer Singh Jul 27 '23 at 05:59
  • pmset -g | grep SleepDisabled doesn't return anything on my MBA? – nohillside Aug 01 '23 at 09:10
  • @nohillside strange! it works for my MBA(M1). – Ankit Veer Singh Aug 01 '23 at 09:20