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.
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 trialApple Silicon, macOS 14+. Notarized by Apple. 3.6 MB. Or buy now, $9.99
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 compiler stops mid-step. Xcode,
xcodebuild,swift build,cargo,makeand Python training loops all stop making progress. Docker builds and containers stop with the Mac, because they run on it. - Network steps can break. Package resolution, dependency downloads, remote caches, notarization uploads and anything that streams to a server may hit a timeout or a dropped connection when the Mac wakes.
- Tests can fail for the wrong reason. A test that measures time, waits for a server or drives the iOS Simulator can see the sleep gap as a hang.
- Hours are lost either way. Even when the process resumes cleanly after wake, the time the lid was shut produced nothing.
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.
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
-iprevents idle system sleep. This is also whatcaffeinatedoes with no flags.-sprevents system sleep, and the manual says it is valid only on AC power.-w PIDattaches to a build that is already running and releases the assertion when it exits, for examplecaffeinate -i -w $(pgrep -n xcodebuild).
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.
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 freeArm 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 trialCoding 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).
- Agent-specific setups. See keeping Claude Code running with the lid closed and keeping Codex running with the lid closed.
- UI tests and the Simulator. With Clamshell's Let agents use the screen while the lid is closed setting on (the default), agents can use the screen during an armed lid-closed session while the built-in display stays black, so the iOS Simulator keeps working. Turning the setting off sleeps the display instead, which uses less battery and keeps the Mac cooler; fine for command-line builds.
- Overnight runs. The power, heartbeat and morning checks in running AI agents overnight on a MacBook apply to long builds too.
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.
- Hard surface, open air. Apple's guidance is a stable work surface with good ventilation, at 10 to 35 °C (50 to 95 °F), and not in a bed, on a pillow or under bedding. Apple notes that Mac laptops get warm in normal use and that fans, where present, turn on automatically. A bag is the worst case: see running a MacBook in a bag.
- Plug in. Long builds drain a battery fast. Clamshell works on battery, and its optional Disarm on low battery (off by default) lets the Mac sleep below a level you choose. QA1340 also lists a low battery and a thermal emergency as reasons macOS forces sleep on its own. More in running lid-closed on battery.
- See what happened.
pmset -g logshows the history of sleeps and wakes, andpmset -g thermlogshows thermal notifications that affect CPU speed, per thepmsetmanual. More on heat and battery care in is it safe to run a MacBook with the lid closed.
Checklist for a lid-closed build
- Prefetch with the lid open. Resolve packages, pull images and download dependencies first, so the long part does not depend on the network.
- Plug in and put the Mac on a hard, open surface.
- Pick the right tool. Lid open:
caffeinate -iaround 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. - Log to a file, for example
xcodebuild -scheme MyApp test 2>&1 | tee build.log, so you can read the result later. - 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.
- Afterwards, read the log end and
pmset -g logto 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
- Why caffeinate does not work with the lid closed
- Run AI coding agents overnight on a MacBook
- Keep Claude Code running with the MacBook lid closed
- Keep Codex running with the lid closed
- Is sudo pmset disablesleep 1 safe?
- Run a MacBook with the lid closed on battery
- Is it safe to run a MacBook with the lid closed?
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.