Does Xcode keep building when you close your MacBook lid?

By Matyas Rathonyi. Updated October 15, 2026. Guides

No: closing a MacBook lid puts the Mac to sleep, so an Xcode or xcodebuild build, swift build, cargo build or training run stops making progress until you open it again. caffeinate -i keeps a long build awake only while the lid is open. To keep building with the lid shut, use Apple's clamshell mode with an external display, or arm Clamshell for Mac with nothing attached.

Lid closedThe Mac sleeps, the build pauses
caffeinate -iIdle sleep only, lid open
Forced sleepApps can delay it, not stop it
$9.99 onceAfter a 7-day free trial

Clamshell for Mac keeps the Mac awake with the lid closed while armed, so a long build, test run or agent session keeps going. Free for 7 days.

Download free trial

Apple Silicon, macOS 14+. Notarized by Apple. 3.6 MB. Or buy now, $9.99

A build log keeps printing and its progress bar keeps filling while the MacBook lid closes. xcodebuild
A build needs an awake Mac.When the lid closes the Mac normally sleeps and the compiler stops mid-target. Keep it awake and the log keeps scrolling to the end.

You start an archive in Xcode, a xcodebuild test run, a clean swift build, a Rust release build, a docker build or a training script, and you want to fold the laptop and leave. Maybe a coding agent started the build and is looping on test failures. Whether the work finishes depends on one thing: whether the Mac stays awake after the lid goes down.

What happens to a build when the lid closes?

Closing the display is one of Apple's documented ways to put a Mac laptop to sleep. Apple describes a sleeping Mac as turned on but using much less power. Your build does not run while the Mac is asleep:

The reason a busy build does not hold the Mac awake: Apple's developer note QA1340 classes closing the lid as forced sleep and says apps cannot prevent forced sleep, only delay it. Idle sleep, the timer that fires when nobody touches the Mac, is a different path, and that is the only one a running process can block.

With default settings the build stalls at lid close and the Mac sleeps. With Clamshell armed the build keeps going and finishes. Default, lid closed z build paused Clamshell armed build finishes same build, same closed lid
Same build, lid closed. By default the build stops at the moment the lid went down. An awake Mac keeps compiling, linking and testing until the job ends.

Wrap the build in caffeinate, with the lid open

For a long build with the lid up, caffeinate is free and built in. Its manual page uses a build as the example: caffeinate -i make runs make and holds an assertion that prevents idle sleep for as long as that process runs. The same pattern works for any build command:

caffeinate -i xcodebuild -scheme MyApp build
caffeinate -i swift build -c release
caffeinate -i cargo build --release
caffeinate -i docker build -t myimage .
caffeinate -i python train.py

When you wrap a command, the assertion ends when the build ends, so the Mac can sleep normally afterwards. To confirm it is active, run pmset -g assertions in another window while the build runs. None of these flags keeps a closed MacBook awake, because closing the lid is forced sleep, not idle sleep. The details are in why caffeinate does not work with the lid closed.

caffeinate -i blocks the idle timer, so the Mac stays awake. Closing the lid is forced sleep, which caffeinate does not stop. Idle timer runs out caffeinate -i awake Lid closes caffeinate -i z asleep
Two kinds of sleep. caffeinate holds off the idle timer for as long as your build runs. Closing the lid is forced sleep, so the same command no longer helps.

Your options with the lid closed

Lid open, display off, on power

Not lid-closed, but free and simple. Set Turn display off on power adapter when inactive in System Settings, Lock Screen, and turn on Prevent automatic sleeping on power adapter when the display is off in Battery, Options, from Apple's sleep settings guide. Add caffeinate -i for a single build.

External display (clamshell mode)

Apple keeps a closed MacBook awake when it is connected to power and an external display and you use an external keyboard and mouse (Apple). Free if you already build at a desk. It ends when you unplug and leave.

sudo pmset disablesleep 1

Stops lid sleep with nothing attached, but it is a global admin switch that is not tied to your build, has no timer and no heat check, and does not appear in the current pmset manual. Forget it and the next closed lid in a bag keeps running. Read pmset disablesleep explained first.

Clamshell for Mac

Keeps an Apple Silicon MacBook on macOS 14 or later awake with the lid closed while armed, with no external display, sudo or admin rights, and always-on thermal safety. Normal lid sleep returns when you disarm or quit, and a restart always restores it. $9.99 once after a 7-day free trial.

Try it free

Arm around a build from the terminal

Clamshell has a URL scheme and Shortcuts actions (Arm, Disarm, Toggle and Status). A small wrapper arms it, runs the build, and disarms when the build exits:

open "clamshell://arm"
xcodebuild -scheme MyApp test
open "clamshell://disarm"

The arm request is asynchronous, so check the menu bar state before you close the lid. Clamshell does not detect xcodebuild, cargo or any other tool, so it will not disarm by itself when a build finishes; the last line or the Disarm Clamshell Shortcuts action does that. While armed, Clamshell also holds an idle-sleep assertion, so you do not need caffeinate on top.

Clamshell keeps your Mac awake with the lid closed. Free for 7 days, then $9.99 once for one Mac. 14-day money-back guarantee.

Download free trial

Coding agents that build and test overnight

An agent such as Claude Code or Codex that fixes a failing test runs the build again and again: xcodebuild test, swift test, cargo test, npm test. All of that runs on your Mac, so it stops when the Mac sleeps, and the agent may read a timed-out build as a real failure. Claude Code holds off idle sleep with caffeinate, which helps with the lid open and not with it closed (details).

Heat during long builds

A full build or a training run keeps every core busy, often for a long time. That makes the same heat with the lid closed as with it open, so placement matters.

A thermal gauge rises during a long build. At serious or critical thermal pressure Clamshell disarms toward normal lid sleep. Thermal pressure serious: disarm
Thermal safety is always on. If macOS reports serious or critical thermal pressure, Clamshell disarms toward normal lid sleep and blocks arming until the Mac cools. That protects the Mac, and it also means an overheating build pauses rather than finishes.

Checklist for a lid-closed build

  1. Prefetch with the lid open. Resolve packages, pull images and download dependencies first, so the long part does not depend on the network.
  2. Plug in and put the Mac on a hard, open surface.
  3. Pick the right tool. Lid open: caffeinate -i around the build. Lid closed at a desk: clamshell mode. Lid closed with nothing attached: arm Clamshell, with external displays disconnected, since Clamshell pauses instead of arming while one is connected.
  4. Log to a file, for example xcodebuild -scheme MyApp test 2>&1 | tee build.log, so you can read the result later.
  5. Close the lid and check once from another device, or over SSH, that the log is still growing. See SSH into a MacBook with the lid closed.
  6. Afterwards, read the log end and pmset -g log to confirm the Mac never slept. Disarm, or let your wrapper do it.

Frequently asked questions

Does Xcode keep building when I close my MacBook lid?

No, not by default. Closing the lid puts a MacBook to sleep, and a sleeping Mac does not run your build. Xcode, xcodebuild, swift build, cargo and Docker builds all stop making progress until the Mac wakes.

Does caffeinate keep a build running with the lid closed?

No. caffeinate -i xcodebuild holds off idle sleep for as long as the build runs, which is the right tool with the lid open. Apple treats closing the lid as forced sleep, which apps can delay but not prevent, so caffeinate does not keep a closed MacBook awake.

Will my build continue when I open the lid again?

The build process picks up when the Mac wakes, but steps that were talking to the network or waiting on a timer during the gap can fail, such as package downloads, remote caches and test timeouts. Read the end of the log instead of assuming it succeeded.

How do I keep a long build running with the MacBook lid closed?

Use Apple's clamshell mode with power, an external display, keyboard and mouse, or keep the Mac awake without a display. Clamshell for Mac does that while armed, with no sudo or admin rights, and disarms toward normal sleep at serious or critical thermal pressure.

Is it safe to run a long build with the lid closed?

Yes on a hard, ventilated surface with the power adapter connected. Apple asks for a stable surface with good ventilation and not a bed, pillow or bedding. Never run a long build in a bag.

Related guides

Sources checked October 15, 2026: Apple's guides on sleep, sleep and wake settings, external displays with a closed lid, and laptop operating temperatures; Apple Technical Q&A QA1340 on sleep and wake notifications; and the macOS caffeinate and pmset manual pages. Clamshell behavior is described from its published facts. Clamshell is an independent app, not affiliated with or endorsed by Apple. Xcode is a trademark of Apple Inc.; Docker, Rust and other product names belong to their owners.