Keep Claude Code running with the lid closed.

Written and tested by Matyas Rathonyi. Updated July 12, 2026. Guides

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

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.

Hold Option and close the lid. The blue state confirms that Clamshell is armed.

Clamshell keeps your Mac awake with the lid closed. Free for 7 days, then $9.99 once.

Download free trial

Arm 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:

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.