You start a long agent run, close the MacBook, and come back to a stalled session. That is macOS working as designed: closing the lid puts the Mac to sleep within seconds, which freezes every process and drops the network. This guide covers why that happens and how to run lid-closed cleanly, with no external display and no admin rights.
Why the run dies when the lid closes
Lid-close sleep is not idle sleep. Your Mac can be at 100% CPU with an agent mid-task and it will still sleep the moment the lid shuts, because the lid switch triggers its own sleep path. Once asleep, Claude Code, Codex, builds, downloads, and SSH sessions all stop making progress. Depending on the tool, the session may or may not recover when the Mac wakes; the hours in between are lost either way.
What doesn't work, and why
caffeinateand most keep-awake menu bar apps prevent idle sleep only. The lid-close sleep path ignores them, so the run still dies. Some apps, like KeepingYouAwake, state outright that closed-lid operation is not supported.sudo pmset -a disablesleep 1does work, but it is a global, admin-level switch: it disables every kind of sleep until you remember to undo it, on battery too. Forget it and the Mac runs itself flat in your bag.- An external display or an HDMI dummy plug satisfies macOS's official clamshell mode, but that is hardware you have to own, power, and carry.
The two-second fix
Clamshell is a menu bar app built for exactly this. Hold Option and close the lid: the screen goes dark, the Mac stays awake, and your agent keeps working. Open the lid and it wakes instantly and disarms itself: arming is momentary, one lid-close at a time (there is a Stay-on option if you want it persistent). It needs no admin rights: no sudo, no kernel extension, no privileged helper.
Clamshell keeps your Mac awake with the lid closed. Free for 7 days, then $9.99 once.
Download free trialArm it from the terminal
For scripted agent workflows, Clamshell has a URL scheme, so the arming can live inside the job itself: arm before the long task, disarm when it exits, even if it fails.
set -e
open "clamshell://arm"
trap 'open "clamshell://disarm" || true' EXIT
# your long-running task
claude -p "run the test suite and fix the failures"
Then close the lid whenever you like; the arm is already set, no Option key needed. clamshell://toggle exists too, and Clamshell's Shortcuts actions (Arm, Disarm, Toggle, Status) can be wired into your own shortcuts and automations.
Claude Code, Codex, Cursor, builds, and SSH
The lid does not care which process is doing the work. If the Mac sleeps, local coding agents pause, child processes stop, network sockets can disconnect, and remote sessions can time out. Keeping the machine awake preserves the whole working environment:
- Claude Code and Codex continue tool calls, tests, and edits instead of waiting for the lid to reopen.
- Cursor and other desktop agents stay alive with their local terminals and background tasks.
- Builds, test suites, downloads, and renders continue using the same CPU and network resources they had with the lid open.
- SSH, screen sharing, and remote access remain reachable because the Mac stays awake and on the network.
Overnight runs, on battery too
Closing the lid turns the screen off, which is most of a laptop's idle power draw. The Mac otherwise runs exactly as it would lid-open: light agent work sips power, heavy work draws what it always draws. Clamshell survives plugging and unplugging mid-run, and an optional low-battery switch (off by default, in Settings) lets the Mac go to sleep if the charge falls below a level you choose, so an overnight run does not have to run the battery flat. The Mac also stays on the network, so SSH and screen sharing keep working while the lid is shut.
Compatibility
Apple Silicon MacBooks on macOS 14 or later. The Option-and-close gesture uses the lid angle sensor found in the MacBook Air M2 (2022) or later and the 14-inch and 16-inch MacBook Pro (2021) or later; on other Apple Silicon laptops you arm from the menu bar instead. More detail in the FAQ, and if you came here for the general no-monitor question, see clamshell mode without an external display.
Clamshell is an independent app, not affiliated with or endorsed by Anthropic or OpenAI. Claude Code and Codex are trademarks of their respective owners.