Hacker News new | ask | show | jobs
by tristanj 37 days ago
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 comments

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.