Caffeine-like utility to keep you MacBook awake with its lid closed
Posted by clash 1 day ago
Comments
Comment by tristanj 1 day ago
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.Comment by clash 1 day ago
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.
Comment by tristanj 1 day ago
#!/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.Comment by clash 1 day ago
My first attempt was a shell script almost exactly like yours. Then I thought I want to be able to set SUID so I don't have to use sudo. This ruled out the shell script. I could have just done a bunch of 'system("pmset ...")' calls but thought I might as well use the native commands.
Comment by nomel 1 day ago
Comment by clash 1 day ago
I did not want GUI, just super simple caffeinate-like CLI. Also I wanted auto-shutdown afterwards.
Comment by smcleod 1 day ago
Comment by mathfailure 1 day ago
Comment by nomel 1 day ago
https://www.google.com/search?q=site%3Agithub.com+keep+mac+o...
First one I used was around 9 years ago. Nearly melted my backpack with the hot Intel CPU!
If you don't want to install anything, you can disable sleep with the built in tools: sudo pmset -a disablesleep 1
Comment by clash 1 day ago
The API call that 'sudo pmset -a disablesleep 1' does is literally the first thing caffeine+ does. But is only part of the full solution: As soon as the lid closes the command is automatically reverted so you can't forget to do it yourself. Also if you kill caffeinate+, the MacBook is put to sleep when the lid is still closed.
Comment by nomel 1 day ago
Comment by clash 1 day ago
You forgot to argue about the LOC vs file count issue.
Also, you are wrong: None does auto sleep on script exit.
Comment by nomel 21 hours ago
And, I've been using these for around 9 years. A few I tried most definitely did re-enable on exit. Not sure why file count is a concern, or especially LOC (a truly horrendous metric to target, with the outcome being unintelligible code golf when optimized for). All that I used were exactly one file I was concerned with: a binary or shell script.
This has become strange. Cheers!
Comment by clash 2 hours ago
Comment by randomblock1 1 day ago
Comment by semiquaver 1 day ago
Comment by clash 1 day ago
Comment by nomel 1 day ago
sudo bash -c 'trap "pmset -a disablesleep 0" EXIT; s=; while :; do if lsof -nP -iTCP:22 -sTCP:ESTABLISHED >/dev/null 2>&1 || pgrep -xq ssh;
then n=1; else n=0; fi; [ "$n" != "$s" ] && pmset -a disablesleep $n && s=$n; sleep 10; done'Comment by tstrimple 11 hours ago
Comment by olyjohn 1 day ago
Comment by WarOnPrivacy 1 day ago
This isn't my first reading about the struggles MacBook users have here. It is bizarre to me that Apple doesn't natively and reliably support closed+awake devices; it feels like they've dug in their heels and I can't imagine why.
Comment by el_benhameen 1 day ago
Comment by cosmic_cheese 1 day ago
Comment by clash 1 day ago
This can be handy when you have a long running task active but need to close your MacBook – for example to change trains, head home from a café or commute to the office.
Comment by WarOnPrivacy 1 day ago
Small purposeful utilities like this are Glob's own gift to us. Your counterpart in my Windows world might be the guy who wrote AlwaysActiveHours - a util that prevents update-related reboots.
Comment by dlivingston 1 day ago
Nice work.
Comment by pseudosaid 1 day ago
Comment by clash 1 day ago
1) It does not behave like caffeine where I can just kill the process and the behavior is reverted.
2) It does not put the MacBook to sleep automatically.