I wrote a small caffeine-like macOS command-line utility to keep your MacBook awake when you close the lid.
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.
> I wrote a small caffeine-like macOS command-line utility to keep your MacBook awake when you close the lid.
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.
Windows has been surprisingly good about this (lid closed, laptop awake). I have a number of always-closed laptops deployed for customers (+ mother in law). Some are desktops that can be easily moved; others are on-prem devices that are left alone to handle certain tasks.
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.
Windows is so good at this that it refuses to let my work laptop sleep when I close it and put it in my backpack no matter how many settings I try tweaking.
For the desk setup scenario, it's less of a problem since they'll stay awake in clamshell if they've got a power source and are hooked up to an external monitor. I've used my personal MacBook in a hybrid home/work setup like this for years without issue. It's only if you want clamshell on battery and/or with no external monitor that tools like this become necessary.
There are probably 20 of these at this point. I use amphetamine [1] (free on the app store). It has configurable timeouts with optional warnings/notifications, to reduce the ability for your MacBook to melt in your backpack. Can also have it disable when an app closes/download completes/battery percentage, and all sorts of triggers.
Yeah, most of these repositories contain more files than caffeinate+ has lines of code.
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.
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.
Switching between headphone output and speaker output on my system is also just one line. I have no idea what that line is because I created a toggle-able icon in waybar which is a really simple script that switches between the two outputs and shows me the correct icon. I didn't need a "tool" to do it, but the packaging and UX are far better than popping up a terminal any time I want to do this.
Both of those are trivial to do in a few lines of shell. There's no need to write or 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.
You say "No need to write a tool", then immediately write a tool. Brilliant :)
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.
DESCRIPTION
caffeinate creates assertions to alter system sleep behavior. If no assertion flags are specified, caffeinate creates an
assertion to prevent idle sleep. If a utility is specified, caffeinate creates the assertions on the utility's behalf,
and those assertions will persist for the duration of the utility's execution. Otherwise, caffeinate creates the
assertions directly, and those assertions will persist until caffeinate exits.
Available options:
-d Create an assertion to prevent the display from sleeping.
-i Create an assertion to prevent the system from idle sleeping.
-m Create an assertion to prevent the disk from idle sleeping.
-s Create an assertion to prevent the system from sleeping. This assertion is valid only when system is running on
AC power.
-u Create an assertion to declare that user is active. If the display is off, this option turns the display on and
prevents the display from going into idle sleep. If a timeout is not specified with '-t' option, then this
assertion is taken with a default of 5 second timeout.
-t Specifies the timeout value in seconds for which this assertion has to be valid. The assertion is dropped after
the specified timeout. Timeout value is not used when an utility is invoked with this command.
-w Waits for the process with the specified pid to exit. Once the the process exits, the assertion is also released.
This option is ignored when used with utility option.
EXAMPLE
caffeinate -i make
caffeinate forks a process, execs "make" in it, and holds an assertion that prevents idle sleep as long as that
process is running.
35 comments
[ 0.23 ms ] story [ 7.0 ms ] threadThis 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.
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.
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.
[1] https://apps.apple.com/us/app/amphetamine/id937984704
https://www.google.com/search?q=site%3Agithub.com+keep+mac+o...
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.
You forgot to argue about the LOC vs file count issue.
Also, you are wrong: None does auto sleep on script exit.
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!
https://github.com/search?q=caffeinate+lid+close&type=reposi...
I did not want GUI, just super simple caffeinate-like CLI. Also I wanted auto-shutdown afterwards.
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.
And why not use the built-in caffeinate command?
And why do you need root? The built-in caffeinate doesn't need root.
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.
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.
Thank me later.
1) It is an insane bloat if you just want that one feature. 2) There is no CLI, just GUI.
Nice work.
``` $ which caffeinate /usr/bin/caffeinate ```
And there’s a man page for it?
``` $ man caffeinate
CAFFEINATE(8) System Manager's Manual CAFFEINATE(8)
NAME caffeinate – prevent the system from sleeping on behalf of a utility
SYNOPSIS caffeinate [-disu] [-t timeout] [-w pid] [utility arguments...]
DESCRIPTION caffeinate creates assertions to alter system sleep behavior. If no assertion flags are specified, caffeinate creates an assertion to prevent idle sleep. If a utility is specified, caffeinate creates the assertions on the utility's behalf, and those assertions will persist for the duration of the utility's execution. Otherwise, caffeinate creates the assertions directly, and those assertions will persist until caffeinate exits.
EXAMPLE caffeinate -i make caffeinate forks a process, execs "make" in it, and holds an assertion that prevents idle sleep as long as that process is running.SEE ALSO pmset(1)
LOCATION /usr/bin/caffeinate
Darwin November 9, 2012 Darwin ```