lid-awake

Keep a MacBook awake (lid closed or not) while any Claude Code session is working, and re-enable sleep the moment the last session finishes. Multi-session safe, O(1) per event, no daemons, no polling.
How it works
Claude Code hooks call the script on session activity:
| Hook | Call | Effect |
|---|
UserPromptSubmit | lid-awake.sh on | touch .lid-awake-flags/<session_id>, pmset -a disablesleep 1 |
PostToolUse | lid-awake.sh on | re-arm the hold once work resumes (no-op while already holding) |
Notification | lid-awake.sh off | Claude is blocked on you (permission prompt / idle input) — stop holding |
Stop / SessionEnd | lid-awake.sh off | remove the flag; if the flags dir is empty → disablesleep 0 |
Waiting on a human is not work. Stop covers the end of a turn, and Notification covers the mid-turn case — a permission prompt can block for hours, and there's no reason to burn battery through it. PostToolUse puts the hold back the moment the tool actually runs, so approving a prompt and walking away still keeps the Mac awake for the rest of the turn.
The flag directory is the real-time state: one empty file per live session. touch/rm are O(1); sleep is only re-enabled when the last holder leaves. A single 0/1 flag file was considered and rejected — it can't refcount, so the first session to stop would re-enable sleep under a second still-working session (that was the original bug).
Safety nets:
- Crash cleanup — each flag records its session's transcript path; a working session updates its transcript constantly, so a transcript stale for 30 min marks a dead session and its flag is dropped at the next event from any session. A 12h flag-age prune backstops flags with no transcript.
- Battery guard — below 20% on battery power, new holds are skipped (no cooked Mac in a closed bag); self-heals on the next prompt once charging or above the threshold.
- Network guard — no default route (Wi-Fi off, nothing else up) means Claude can't work anyway, so the hold is skipped and any existing one released. A connected-but-weak signal still has a route and keeps the hold — bad reception is often temporary.
- Either guard tripping mid-session releases that session's hold; other live sessions keep the Mac awake.
- Reaper — every cleanup above is triggered by a hook, and a killed terminal (cmux window closed, tab force-quit, SSH dropped) fires none: the flag survives and the Mac never sleeps again. A launchd agent runs
lid-awake.sh reap every 5 min — no claude process anywhere means no flag can legitimately be holding. Install once with bash lid-awake.sh install-reaper; status tells you if it's missing.
- Log at
~/.claude/scripts/lid-awake.log, self-truncated at ~200KB.
status / clear subcommands for debugging and manual unstick.
Install (fast — as a Claude Code plugin)
The repo is a self-hosting plugin: hooks are registered automatically, no settings.json editing.
-
In Claude Code:
/plugin marketplace add LeonMelamud/lid-awake
/plugin install lid-awake@lid-awake
-
Allow the two exact pmset commands without a password (one-time, the only manual step — no plugin can or should automate sudoers):
echo "$USER ALL=(root) NOPASSWD: /usr/bin/pmset -a disablesleep 1, /usr/bin/pmset -a disablesleep 0" \
| sudo tee /etc/sudoers.d/lid-awake >/dev/null && sudo chmod 440 /etc/sudoers.d/lid-awake \
&& sudo visudo -c
The final visudo -c must print "parsed OK" — a malformed sudoers file can break sudo itself, so if it complains, remove the file (sudo rm /etc/sudoers.d/lid-awake) and retry.
Done. Verify with bash lid-awake.sh status after your next prompt.
Install (manual, without the plugin system)
-
Copy the script:
mkdir -p ~/.claude/scripts
cp lid-awake.sh ~/.claude/scripts/lid-awake.sh
-
Add the sudoers entry from step 2 above.
-
Wire the hooks in ~/.claude/settings.json:
{
"hooks": {
"UserPromptSubmit": [
{ "hooks": [{ "type": "command", "command": "bash ~/.claude/scripts/lid-awake.sh on", "async": true }] }
],
"PostToolUse": [
{ "hooks": [{ "type": "command", "command": "bash ~/.claude/scripts/lid-awake.sh on", "async": true }] }
],
"Notification": [
{ "hooks": [{ "type": "command", "command": "bash ~/.claude/scripts/lid-awake.sh off", "async": true }] }
],
"Stop": [
{ "hooks": [{ "type": "command", "command": "bash ~/.claude/scripts/lid-awake.sh off", "async": true }] }
],
"SessionEnd": [
{ "hooks": [{ "type": "command", "command": "bash ~/.claude/scripts/lid-awake.sh off", "async": true }] }
]
}
}
Debugging