It is not ideally suited, however this is an answer. To stop the laptop computer from sleeping when the lid is closed and also you’re operating on battery, run the next instructions:
sudo pmset -b sleep 0; sudo pmset -b disablesleep 1
To re-enable laptop computer sleeping when the lid is closed and also you’re operating on battery, run the next instructions:
sudo pmset -b sleep 5; sudo pmset -b disablesleep 0
The “5” within the second set of instructions represents the variety of minutes earlier than sleeping when on battery; modify as desired in your laptop computer.
This can be a bit harmful, since should you neglect to re-enable your settings, the laptop computer won’t ever sleep when on battery. Due to this, I’ve written a shell script to robotically re-enable the settings:
#!/bin/bash
#***************************************************************************
#*** noz - forestall laptop computer from sleeping when lid is closed
#***************************************************************************
#***** set some defaults *****
BATTERY_SLEEP=5 # in minutes
DEF_WAKE_LEN=300 # in seconds
#***** decide timeout worth *****
timeout_len=${1:-$DEF_WAKE_LEN}
perform prevent_sleep() {
echo
echo -n "Stopping sleep for $timeout_len seconds; press to proceed..."
sudo pmset -b disablesleep 1
sudo pmset -b sleep 0
}
perform enable_sleep() {
# $1: = 0, timeout = 1, Ctrl-C = undef
#----- insert a newline for timeout or Ctrl-C -----
if [[ ${1:-1} -eq 1 ]]; then echo; fi
echo "Restoring earlier battery sleep setting: $BATTERY_SLEEP"
sudo pmset -b disablesleep 0
sudo pmset -b sleep $BATTERY_SLEEP
#----- sleep on timeout solely -----
if [[ ${1:--1} -eq 1 ]]; then sudo pmset sleepnow; fi
exit
}
#***** forestall it from sleeping *****
prevent_sleep
#***** lure Ctrl-C *****
lure enable_sleep INT
#***** look forward to an enter *****
learn -t $timeout_len
rc=$?
#***** re-enable regular sleep *****
enable_sleep $rc
The shell script will disable sleeping till you hit the Enter key, at which level it would re-enable the sleep settings (alternately, you’ll be able to hit Ctrl–C and obtain the identical factor). It’ll additionally set a timeout (defaults to 300 seconds/5 minutes) after which the sleep settings will robotically be re-enabled, and the laptop computer will likely be pressured to fall asleep. Whereas this could be a ache should you’re utilizing your laptop computer in a gathering, it will likely be a lifesaver should you forgot and put your laptop computer in your bag to go residence.
Astute readers will word that these instructions require sudo
; sadly, that is unavoidable AFAIK. What I’ve completed on my system is to make it in order that I haven’t got to enter my password to run pmset
as root. To try this, edit the sudoers
file (sudo visudo
) and add this line:
joe ALL=(ALL) NOPASSWD: /usr/bin/pmset
changing “joe” along with your username. You might in all probability obtain the identical outcome (i.e. operating the script with out having to enter your password) by operating the shell script SETUID, however I do not like doing that; opening up this one command through sudoers appears much less dangerous to me.
To run the script, stick it in a listing in your PATH
and invoke it as such:
noz []
If you get to the place you are going, merely hit Enter or Ctrl–C and also you’re good to go. And should you neglect about it, it would robotically reset and sleep.
There’s in all probability a method to obtain all of this through AppleScript, as a way to then assign it a sizzling key and what not; I will attempt that if I ever get uninterested in operating this from the command line.