tristanj
5 hours ago
Why download a third-party script when this feature is built into MacOS? Just run:
sudo pmset -a disablesleep 1
to prevent macOS from sleeping. This works even when you close the lid and are on battery power. To re-enable normal sleep again, change the 1 to a 0.clash
3 hours ago
1) Because you will eventually forget to reactivate it, which then leads to obvious problems.
2) Because you can for example be connected via ssh: You run the script and if your connection is disconnected, caffeinate+ shuts down the MacBook.
Edit: The API call that 'sudo pmset -a disablesleep 1' does is literally the first thing caffeine+ does. As soon as the lid closes the command is automatically reverted so you can't forget to do it yourself.
tristanj
a few seconds ago
Both of those are trivial to do in a few lines of shell. There's no need to download a custom tool for this. I asked Opus to draft one up and it's just five lines long.
#!/bin/bash
[ "$EUID" = 0 ] || exec sudo "$0" "$@"
pmset -a disablesleep 1
trap exit HUP INT TERM
trap 'pmset -a disablesleep 0; ioreg -r -k AppleClamshellState | grep -q "State\" = Yes" && pmset sleepnow' EXIT
"${@:-read}"
How to use it ./caf.sh # stay awake until you press Enter
./caf.sh make build # stay awake only while the command runs
Sleep is re-enabled when: the command finishes, the user presses Enter, on Ctrl-C, on SSH disconnect, or on kill (excluding SIGKILL). If the lid is closed when the command finishes, sleep is re-enabled and the Mac goes to sleep immediately.