Hacker Dudes// alternate deck. same feed.
FEED ~6/h Δ0 ────:──── LINK ░ SIGNAL
99%
SPOOLED 0/45 CYCLE 61340.103.56.17
Caffeine-like utility to keep you MacBook awake with its lid closedgithub.com
17pts/31 comments/3d/─
by clash / hn> / « feed
▲▼ clash18:54 hn>

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.

▲▼ WarOnPrivacy19:08 hn>
>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.

▲▼ clash19:16 hn>

Haha, every operating system has its quirks. The auto reboot is certainly a very nasty one.

▲▼ hurfdurf08:34 hn>

"Active Hours"? Who comes up with that kind of stuff? And you cannot even disable it, only out"smart" it. Unbelievable.

▲▼ WarOnPrivacy19:04 hn>

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.

▲▼ el_benhameen23:22 hn>

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.

▲▼ cosmic_cheese23:29 hn>

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.

▲▼ olyjohn21:40 hn>

This is neat. I ended up having to buy one of those dummy HDMI dongles to keep my MacBook awake so that I could remote into it. If it thinks there's an external monitor attached, it won't sleep. Or at least you can set it up that way.

▲▼ nomel22:05 hn>

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 (like bluetooth presence, useful for backpack proximity).

[1] https://apps.apple.com/us/app/amphetamine/id937984704

▲▼ mathfailure23:22 hn>

Are those 20 free and open source as well? Name them then.

▲▼ nomel23:30 hn>

My mistake. Well over 20.

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

─ 5 more ─
▲▼ semiquaver23:41 hn>

What are you talking about? It’s a one line terminal invocation. These apps are utterly useless. Of course there’s a million open source ones.

─ 3 more ─
▲▼ clash01:25 hn>

Yeah, I obviously also did the research prior to implementing my solution.

I did not want GUI, just super simple caffeinate-like CLI. Also I wanted auto-shutdown afterwards.

▲▼ pseudosaid22:05 hn>

amphetamine has been around forever.

▲▼ clash01:13 hn>

I found this before I implemented my solution and it did not solve my problem:

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.

▲▼ tristanj23:30 hn>

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.

▲▼ clash01:08 hn>

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.

▲▼ tristanj04:07 hn>

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.

─ 1 more ─
▲▼ sys_6473823:58 hn>

brew install vorssaint

Thank me later.

▲▼ clash01:16 hn>

Nope, not thanking you now or later.

1) It is an insane bloat if you just want that one feature. 2) There is no CLI, just GUI.

▲▼ dlivingston01:59 hn>

Just read through the source code. Very simple and the first non-AI coded project I've seen here in a while.

Nice work.