Hacker News new | ask | show | jobs
by clash 37 days 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.

1 comments

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.