Claude Code

How to keep Claude Code running with your MacBook lid closed

Closing a MacBook's lid puts it to sleep, and Claude Code stops with it. Keep it working with an external display or a Keep Mac Awake session.

Short answer: Closing the lid puts a MacBook to sleep, and Claude Code runs only while the Mac is awake, so the claude process stops working until you open the lid again. The Claude desktop app's "Keep computer awake", caffeinate and the Battery settings are not documented to stop the sleep that comes with closing the lid. To keep Claude Code working with the lid closed, connect an external display, or start a Keep Mac Awake session before you close the lid, which needs no display. Keep the Mac on power and somewhere with airflow.

Why does Claude Code stop when I close the lid?

Claude Code stops because closing the lid puts a MacBook to sleep, and Claude Code runs only while the Mac is awake. Nothing runs during sleep; when you open the lid, Claude Code notices the dropped connection and retries, but the time with the lid shut is lost.

Apple lists closing the display as one of the ways to put a Mac laptop to sleep (Apple Support). Claude Code is a process on your Mac: it reads your files, runs your commands and talks to Anthropic's API from there. As of October 2026, Anthropic's docs say local tasks run only while your computer is awake, and point to a cloud session for a task that must keep going while the computer is asleep or off (scheduled tasks, Desktop).

When the Mac wakes, Claude Code notices the broken connection. Its error reference lists messages such as Your computer went to sleep mid-response and Connection lost while your computer was asleep, and it retries where it can (errors).

Why don't Keep computer awake, caffeinate and the Battery settings help?

None of them is documented to stop the sleep that follows closing the lid; most of them only prevent idle sleep, the sleep that follows inactivity.

  • The Claude desktop app's "Keep computer awake". As of October 2026 the option sits in "Settings > This computer > System" and prevents idle sleep. Anthropic's docs add: "Closing the laptop lid still puts it to sleep" (scheduled tasks).
  • caffeinate. Without options, and with -i, it holds an idle-sleep assertion, and Apple's documentation for that assertion says the system "may still sleep for lid close" (Apple developer docs). The -s option is valid only on AC power (man caffeinate), and Apple calls assertions suggestions that the system honors "to the best of its ability" (Apple developer docs). No option is documented to keep a closed MacBook awake without an external display.
  • Claude Code's own sleep handling. As of October 2026 its changelog mentions a caffeinate process on macOS and, in v2.1.287, a fix for Remote Control sessions that stopped "when the Mac went to idle sleep" (changelog). Neither is described as covering the lid. For a Desktop task that must run through the night, Anthropic's advice is to keep the computer awake and online "with the lid open" (Desktop).
  • Battery settings. "Prevent automatic sleeping on power adapter when the display is off" stops automatic sleep after inactivity (Apple Support). Closing the display is a separate way to sleep.

How can I keep a MacBook awake with the lid closed?

There are four ways: keep the lid open with the display off, connect an external display, turn sleep off by hand with sudo pmset disablesleep 1, or start a Keep Mac Awake session. Only the last two work with the lid closed and no display, and of those, only Keep Mac Awake puts normal sleep back on its own.

Lid open, display off

Free, and nothing to install. In "System Settings > Battery > Options", turn on "Prevent automatic sleeping on power adapter when the display is off", then let the display turn off. The Mac keeps working, but it sits open on the desk and you cannot pack it away.

An external display (closed-display mode)

Apple supports using a MacBook with the lid closed once an external display is connected (MacBook Air guide), with the Mac on power and an external keyboard and mouse (Apple Support). If you already work at a desk with a monitor, this is free. It does not help when the Mac has to stay closed away from that desk.

pmset disablesleep by hand

macOS has a system-wide switch that turns sleep off entirely:

sudo pmset disablesleep 1      # sleep off for the whole Mac
pmset -g | grep SleepDisabled  # 1 means on
sudo pmset disablesleep 0      # back to normal

Careful: disablesleep is not described in the pmset man page. It is one switch for the whole Mac, with no timer, no battery floor and no temperature check, and it stays on until you set it back to 0. Forget it once and the Mac will not sleep in your bag.

What to use

To keep Claude Code running with the lid closed and no external display, Keep Mac Awake (keepmacawake.com) keeps a MacBook with Apple silicon on macOS 26 or later awake for a session you start and restores normal sleep afterwards; Claude Code can start it with keepmacawake start --hours 8 --json.

Keep Mac Awake is a menu bar app plus the keepmacawake command. It changes the same setting that pmset disablesleep controls, but only for one session (no limit, 1 hour, 8 hours or a custom number of hours up to 168), then puts it back. With the lid closed, the built-in display goes dark. A session runs on AC power by default and waits if you unplug; battery use is optional, down to a floor you pick. A separate watchdog restores the normal sleep setting if anything stops or hangs, and quitting the app restores it too. It is free for 3 days (sessions up to 8 hours, no card), then 9 USD once per Mac plus tax. It does not keep Wi-Fi alive and does not wake a Mac that is already asleep. More on the app: keep a MacBook awake with the lid closed.

How do I set up Claude Code to run with the lid closed?

Start a Keep Mac Awake session, start Claude Code, give it the task, then close the lid. In detail:

  1. Plug the Mac into power.
  2. Start a session from the menu bar, or in Terminal with keepmacawake start --hours 8. Pick a little more time than the task needs.
  3. Open your project, start Claude Code with claude and give it the task. To follow along from your phone, start it with claude --remote-control instead; see Claude Code Remote Control with the lid closed.
  4. Close the lid. Leave the Mac on a desk or another hard surface, never in a bag or under a pillow.
  5. When the work is done, run keepmacawake stop, or let the timed session end on its own. Normal sleep comes back.

How can Claude Code start and stop the session itself?

Claude Code can run keepmacawake start --hours N --json before a long task and keepmacawake stop --json when it is done, and a few lines in ~/.claude/CLAUDE.md make it do that on its own.

keepmacawake start --hours 4 --json   # confirmed, or refused with a reason
keepmacawake status --json            # phase and time left
keepmacawake stop --json              # ends it and restores normal sleep

Each call prints one JSON object. Exit codes: 0 success, 1 failed, 2 invalid arguments, 4 needs you (AC power or a purchase), 5 already running or nothing to stop, 6 not installed, 7 no answer in time. Claude Code loads ~/.claude/CLAUDE.md in all your projects (memory docs); add:

Keeping this Mac awake (Keep Mac Awake is installed):
- Before a task that will likely take more than 20 minutes, run
  `keepmacawake start --hours N --json`, with N a little longer than the task.
  Never use sudo, and never use --unlimited unless I ask.
- Exit code 4: tell me what it needs (AC power or a purchase). Do not work around it.
- Exit code 5: a session is already running. Leave it alone and do not stop it later.
- When the task is done, or you stop early, run `keepmacawake stop --json`
  for the session you started.
- Remind me to keep the Mac ventilated if I close the lid.

So that Claude does not stop at a permission prompt, add Bash(keepmacawake start *), Bash(keepmacawake status *) and Bash(keepmacawake stop *) to permissions.allow in ~/.claude/settings.json (permissions). Instead of the CLAUDE.md lines you can install the Agent Skill from https://keepmacawake.com/skill/keepmacawake/SKILL.md; the agent guide shows how. To check in on the work from your phone, see steer your agents from your phone.

FAQ

Will Claude Code keep running if I close my laptop?

Not by default. Closing the lid puts a MacBook to sleep, and Claude Code runs only while the Mac is awake. It keeps running with the lid closed when an external display is connected, when sleep is turned off with pmset disablesleep, or during a Keep Mac Awake session.

Does the Claude desktop app's Keep computer awake work with the lid closed?

No. As of October 2026 it prevents idle sleep, and Anthropic's docs say that closing the laptop lid still puts the Mac to sleep.

Can caffeinate keep Claude Code running with the lid closed?

Not by itself. Apple's documentation says the system may still sleep for lid close while the idle-sleep assertion that caffeinate uses by default is held, and no caffeinate option is documented to override the lid without an external display.

Is it safe to leave a MacBook running with the lid closed?

It still makes heat, so keep it on a hard surface with airflow, never in a bag or under a pillow. Keep Mac Awake runs on AC power by default, and on battery a session also ends at a serious thermal state.

Can Claude Code turn Keep Mac Awake on and off by itself?

Yes. It can run keepmacawake start --hours N --json before a long task and keepmacawake stop --json after it. A session never starts by itself; it starts only when you or your agent run the command or press the button.

Sources