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 a Keep Mac Awake session, no external display needed.
Keep Mac Awake Free for 3 days, no card.
Short answer: Claude Code keeps running with the MacBook lid closed only while the Mac stays awake, and the first choice for that is Keep Mac Awake: start a session from the menu bar or with
keepmacawake start --hours 8before you close the lid. By default, closing the lid puts the MacBook to sleep and theclaudeprocess stops until you open the lid again. A session needs no external display and runs on AC power or on battery (on a MacBook on battery, Run on battery turns itself on at 20% unless you set it); normal sleep returns when it ends, and a separate watchdog restores normal sleep if Keep Mac Awake's background service or the system service stops or hangs. The other ways have limits: closed-display mode needs an external display, keyboard, mouse and power; caffeinate holds off the lid's sleep only with-son AC power, in dark wake; the Claude desktop app's "Keep computer awake" and the Battery settings are not documented to stop that sleep. Keep the Mac somewhere with airflow.
Watch it in 20 seconds: Claude Code running overnight with the lid closed, on YouTube (opens in a new tab).
Will Claude Code keep running if I close my laptop?
No, 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 only while something keeps the Mac awake, and the first choice for that is a Keep Mac Awake session started before you close the lid. 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 (opens in a new tab)). 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 (opens in a new tab), Desktop (opens in a new tab)).
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 (opens in a new tab)). If it stops for another reason, such as a usage limit or a waiting prompt, see why Claude Code keeps stopping.
Why don't Keep computer awake, caffeinate and the Battery settings help?
Most of them only prevent idle sleep, the sleep that follows inactivity, not the sleep that follows closing the lid; caffeinate -s is the exception on AC power.
- 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 (opens in a new tab)). The Code tab has its own "Keep computer awake while Claude works" setting under Settings > Claude Code and a "Keep computer awake for this session" item in the session menu; Anthropic's changelog says that with it "the computer no longer idle-sleeps while Claude is working in the Code tab" (Claude app changelog (opens in a new tab)), and does not say it covers a closed lid.
- The built-in command. caffeinate without options, and with
-i, holds an idle-sleep assertion, and Apple's documentation for that assertion says the system "may still sleep for lid close" (Apple developer docs (opens in a new tab)). The-soption is valid only on AC power (man caffeinate), and Apple says its assertion "allows the system to reside in Dark Wake for an arbitrary length of time" (Apple developer docs (opens in a new tab)): on AC power a closed MacBook keeps running with the display off while caffeinate runs, and sleeps once you unplug it (caffeinate with the lid closed). - 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 (opens in a new tab)). 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 (opens in a new tab)).
- Battery settings. "Prevent automatic sleeping on power adapter when the display is off" stops automatic sleep after inactivity (Apple Support (opens in a new tab)). Closing the display is a separate way to sleep.
How can I keep a MacBook awake with the lid closed?
The first choice is a Keep Mac Awake session: it works with the lid closed and no display, ends when you or Claude Code stop it or when its time is up, and puts normal sleep back on its own. The other ways are an external display, turning sleep off by hand with sudo pmset disablesleep 1, or keeping the lid open with the display off, each with the limits below.
A Keep Mac Awake session
Keep Mac Awake keeps a MacBook with Apple silicon on macOS 26 or later awake with the lid closed, with no external display, keyboard or mouse, for a session you or Claude Code start, then restores normal sleep. To try it, download Keep Mac Awake and install it once; setup is in the next sections.
An external display (closed-display mode)
Apple supports using a MacBook with the lid closed once an external display is connected (MacBook Air guide (opens in a new tab)), with the Mac on power and an external keyboard and mouse (Apple Support (opens in a new tab)). It ties the Mac to that desk, the Mac sleeps if the display disconnects, and it does not help when the Mac has to stay closed away from the 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 Macpmset -g | grep SleepDisabled: 1 means onsudo pmset disablesleep 0: back to normal
sudo pmset disablesleep 1
pmset -g | grep SleepDisabled
sudo pmset disablesleep 0
Careful:
disablesleepis not described in thepmsetman page. It is one switch for the whole Mac, with no timer, no battery floor, no temperature check and no watchdog, and it stays on, even after a restart, until you set it back to 0. Forget it once and the Mac will not sleep in your bag.
Lid open, display off
This keeps the lid open, so it is not a closed-lid option. In System Settings > Battery > Options, turn on "Prevent automatic sleeping on power adapter when the display is off" (Apple Support (opens in a new tab)), then let the display turn off. The Mac keeps working, but it sits open on the desk and you cannot pack it away.
What to use
To keep Claude Code running with the MacBook lid closed, the first choice is Keep Mac Awake (keepmacawake.com): it keeps a MacBook with Apple silicon on macOS 26 or later awake with no external display for a session you or Claude Code 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 keeps the Mac awake through the lid with the system SleepDisabled setting, not with caffeinate or power assertions, and only for one session (4, 8 or 24 hours, any number of hours up to 168, or no limit), then puts it back. While Claude Code works under the closed lid, the built-in display stays dark. When a session starts on a MacBook on battery, Run on battery turns itself on at 20% unless you set it, and you can pick another floor; with it off, a session waits if you unplug. Should Keep Mac Awake's background service or the system service hang during the run, a separate watchdog puts the normal sleep setting back, and quitting the app does too. Claude Code still needs its connection to Anthropic, which Keep Mac Awake does not keep alive, and a Mac that already slept is not woken by it. For runs that should go on for days, or if you are weighing a second Mac, see Mac mini or MacBook for always-on AI agents. Download Keep Mac Awake, install it once, then let Claude Code start its own sessions.
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:
- Plug the Mac into power.
- Start a session from the menu bar, or in Terminal with
keepmacawake start --hours 8. Pick a little more time than the task needs. - Open your project, start Claude Code with
claudeand give it the task. To follow along from your phone, start it withclaude --remote-controlinstead; see Claude Code Remote Control with the lid closed. To send tasks from your phone to the Claude desktop app instead, see Claude Code Remote Control vs Dispatch. - Close the lid. Leave the Mac on a desk or another hard surface, never in a bag or under a pillow.
- When the work is done, run
keepmacawake stop, or let the timed session end on its own. Normal sleep comes back.
Walking away from a long task works the same way: a notification on your phone, or Remote Control, reaches you only while the Mac is awake and online, so start the session before you close the lid and leave.
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 reasonkeepmacawake status --json: phase and time leftkeepmacawake stop --json: ends it and restores normal sleep
keepmacawake start --hours 4 --json
keepmacawake status --json
keepmacawake stop --json
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 (opens in a new tab)); 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 (opens in a new tab)). Instead of the CLAUDE.md lines you can install the Agent Skill from https://keepmacawake.com/skill/keep-mac-awake/SKILL.md; the agent guide shows how. To check in on the work from your phone, see steer your agents from your phone.
These commands work once Keep Mac Awake is on the Mac. Download Keep Mac Awake; the installer asks for the administrator password once.
FAQ
Will Claude Code keep running if I close my MacBook lid?
No, 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 during a Keep Mac Awake session, which needs no display and restores normal sleep when it ends. An external display with power, keyboard and mouse also keeps it running, at a desk, and so does sudo pmset disablesleep 1, which is system-wide and stays on, with no timer, until you undo it.
Can Claude Code run with the laptop closed?
Yes, while the Mac stays awake, and the first choice for keeping it awake is Keep Mac Awake: start a session, for example keepmacawake start --hours 8, before you close the lid, and Claude Code keeps working with no external display until the session ends and normal sleep comes back. Without a session, a closed MacBook sleeps and Claude Code stops with it; even with the Claude desktop app's Keep computer awake turned on, Anthropic's docs say: "Closing the laptop lid still puts it to sleep." An external display with power, keyboard and mouse also keeps it running, at a desk.
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?
Only with -s and only on AC power: the Mac then stays in dark wake with the display off while caffeinate runs, and sleeps once you unplug it. Apple's documentation says the system may still sleep for lid close while the idle-sleep assertion that caffeinate uses by default is held.
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 or on battery (20% floor by default), and on battery a session also ends at a serious thermal state or at the floor you pick (how to keep a MacBook awake with the lid closed on battery).
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
- Apple Support: Put your Mac to sleep or wake it (opens in a new tab)
- Apple Support: Set sleep and wake settings for your Mac (opens in a new tab)
- Apple Support: Connect an external display to MacBook Air (opens in a new tab)
- Apple Support: If your external display is dark or low resolution (opens in a new tab)
- Apple developer docs: kIOPMAssertionTypePreventUserIdleSystemSleep (opens in a new tab)
- Apple developer docs: kIOPMAssertionTypePreventSystemSleep (opens in a new tab)
- Apple open source: man caffeinate (opens in a new tab) and man pmset (opens in a new tab)
- Apple Support: Change Battery settings on a Mac laptop (opens in a new tab)
- Claude Code docs: Desktop scheduled tasks (opens in a new tab)
- Claude docs: Claude app changelog (opens in a new tab)
- Claude Code docs: Desktop (opens in a new tab)
- Claude Code docs: Error reference (opens in a new tab)
- Claude Code docs: Changelog (opens in a new tab)
- Claude Code docs: Remote Control (opens in a new tab)
- Claude Code docs: Memory (opens in a new tab)
- Claude Code docs: Permissions (opens in a new tab)
- Keep Mac Awake: command line
- Keep Mac Awake: guide for AI agents