Mac Guide

How to Keep Claude Code Running With the MacBook Lid Closed

Close the lid on a long Claude Code or Codex task and the Mac goes to sleep, and the task stops with it. Why caffeinate does not prevent that, the three things that do, the exact commands, and what each one risks.

Short answer

Closing a MacBook's lid makes it sleep, and caffeinate's idle-sleep assertion does not stop that. Three things do: run sudo pmset -a disablesleep 1 and undo it with 0 afterwards, use clamshell mode with power and an external display, or use a tool that switches lid sleep off only while an agent is working.

Key points

  • caffeinate -i stops idle sleep only. Closing the lid is a separate trigger, and caffeinate -s applies only on AC power.
  • sudo pmset -a disablesleep 1 keeps a MacBook running with the lid shut, on battery too. sudo pmset -a disablesleep 0 undoes it.
  • Left on, that setting can run the battery to empty, and nothing watches the heat in a closed bag.
  • Clamshell mode is the way Apple documents, but it needs power, an external display, a keyboard and a mouse or trackpad.
  • A short script, or AgentAwake, keeps lid sleep off only for as long as the agent is working.

Why does a MacBook sleep with the lid closed even when caffeinate is running?

Because closing the lid is not idle sleep, and idle sleep is what caffeinate normally prevents. macOS treats "nothing has happened for a while" and "the lid just closed" as two different reasons to sleep. Apple's documentation for the idle-sleep assertion says it plainly: the system may still sleep for lid close, the Apple menu or low battery.

The caffeinate man page lists five kinds of assertion. -d keeps the display on, -i stops idle system sleep, -m stops idle disk sleep, -u declares the user active, and -s prevents system sleep but is "valid only when system is running on AC power". With no flag it holds an idle-sleep assertion. None of them is documented to cover the lid, and on battery -s does not apply at all.

This catches agent users out because Claude Code already runs a caffeinate while it writes a reply. With the lid open the Mac stays awake and looks protected. Close the lid and it sleeps anyway. While it sleeps nothing runs: the agent stops mid-task, and requests that were in flight can time out. Two commands show what is going on:

# What is holding the Mac awake, and with which assertion
pmset -g assertions

# Is lid sleep switched off? 1 = off, 0 = normal
pmset -g | grep SleepDisabled
What happens when you close the lid on a working agent
  1. Agent workingClaude Code holds a caffeinate, so idle sleep is blocked
  2. Lid closesA separate sleep trigger the assertion does not cover
  3. Mac sleepsEvery process pauses, the agent included
  4. Lid opensThe agent resumes, but calls that were in flight may have failed

How do you keep a MacBook awake with the lid closed using pmset?

Run sudo pmset -a disablesleep 1. It turns system sleep off entirely, lid close included, on battery and on power, until you run sudo pmset -a disablesleep 0. It needs your admin password and changes a system-wide setting, not just the current session.

The flag is not in the pmset man page, but it is widely used: pmset -g reports it as SleepDisabled, Amphetamine's Power Protect helper installs a sudo rule for exactly these two commands, and AgentAwake switches the same setting. Check it took effect before you close the lid:

# Before you close the lid
sudo pmset -a disablesleep 1
pmset -g | grep SleepDisabled    # expect: SleepDisabled 1

# When the agent has finished
sudo pmset -a disablesleep 0

What are the risks of pmset disablesleep?

The risk is that it stays on. Nothing in macOS turns it back off when the agent finishes, so a Mac you put in a bag after the job is still awake and still drawing power.

On battery it will run until the battery is empty, which also loses whatever was not saved. With the lid shut and the Mac in a bag, the fans have little air to move and nothing in the setting reacts to temperature. And if you forget it, the Mac stays awake through every lid close afterwards, for days if nobody checks.

Each of those is fixable: undo the setting when the command ends, stop at a battery floor, and react to heat. The script in the next section does the first two for free.

Lid sleep off by hand, or only while an agent works

disablesleep 1 by hand

  • Stays on after the agent finishes
  • Runs the battery down to empty
  • Nothing reacts to heat in a bag
  • A forgotten switch stays on for days

Off only while the work runs

  • Back on when the work stops
  • Sleeps at a battery floor you set
  • Pauses or cools when the Mac runs hot
  • Restored by a watchdog if the tool stops

A safer free version: switch lid sleep off for one command

Wrap the agent in a script that turns lid sleep off, runs it, and turns lid sleep back on however the command ends. First let the three pmset commands run without a password, because sudo forgets your password after a few minutes and the undo at the end of a long job would wait for one with nobody there to type it:

# Once: allow exactly these three pmset commands without a password
sudo visudo -f /etc/sudoers.d/lid-awake
# add this one line, using your short user name (run: whoami)
yourname ALL=(root) NOPASSWD: /usr/bin/pmset -a disablesleep 0, /usr/bin/pmset -a disablesleep 1, /usr/bin/pmset sleepnow
#!/bin/bash
# lid-awake: lid sleep off for one command, back on when it ends
# or when the battery falls to FLOOR %. Usage: lid-awake codex
FLOOR=20
P=/usr/bin/pmset
sudo -n $P -a disablesleep 1 || exit 1
( while sleep 60; do
    b=$(pmset -g batt)
    pct=$(echo "$b" | grep -Eo '[0-9]+%' | head -1 | tr -d %)
    if echo "$b" | grep -q "Battery Power" && [ "$pct" -le "$FLOOR" ]; then
      sudo -n $P -a disablesleep 0 && sudo -n $P sleepnow
      exit
    fi
  done ) &
watcher=$!
disown
restore() {
  kill $watcher 2>/dev/null
  sudo -n $P -a disablesleep 0
  ioreg -r -k AppleClamshellState -d 1 | grep -q '"AppleClamshellState" = Yes' && sudo -n $P sleepnow
}
trap restore EXIT
"$@"
# Install it, then start your agent through it
chmod +x lid-awake
sudo mkdir -p /usr/local/bin && sudo mv lid-awake /usr/local/bin/
lid-awake claude
  • Lid sleep comes back when the agent exits, when you press Ctrl-C, and when the terminal window closes.
  • On battery, a watcher checks once a minute and sends the Mac to sleep at 20%. Change FLOOR to suit.
  • If the lid is still shut when the job ends, pmset sleepnow puts the Mac to sleep instead of leaving it running in the bag.
  • It cannot help after a kill -9, a crash of the shell or a power cut. Run pmset -g | grep SleepDisabled if in doubt.
  • It watches neither heat nor whether the agent is still working. An agent that sits waiting for your approval keeps the Mac awake until you answer.

Does clamshell mode work for AI agents?

Yes, if you are at a desk. Clamshell, or closed-display, mode is the way Apple documents for running a MacBook with the lid shut: connect it to power and to an external display, keyboard and mouse or trackpad, then close the lid and keep working on the external screen. Nothing is changed in the system, so there is nothing to undo.

It does not cover the case most agent users have in mind, which is closing the lid and leaving: on battery, or with nothing plugged in, closing the lid still sleeps the Mac.

How does AgentAwake keep agents running with the lid closed?

AgentAwake is a menu bar app from SyncEdge that does the pmset switch for you, only while an agent is working. An agent counts as working when Codex or opencode, once connected, report through their hooks, when a caffeinate runs inside it, as Claude Code does while it replies, or when its own process is busy on the processor. By default it watches claude, codex, gemini, aider and opencode. When none is working it waits a grace period, 2 minutes by default, and lets the Mac sleep.

It adds the safeguards the manual method lacks. On battery the Mac is allowed to sleep at a floor you choose: 10, 15, 20 or 25%. It pauses whenever macOS reports serious heat. It runs the fans: on the default Balanced profile they hold at least 40% while the lid is closed and agents work, and reach full speed at 90 °C. The panel shows "Lid sleep blocked ✓" only after reading the setting back from the system, and a watchdog turns lid sleep back on within 60 seconds if the app stops.

Setup asks for your password once. The sudo rule it installs covers exactly three pmset commands: lid sleep off, lid sleep on, and sleep now. It needs macOS 14 or later on Apple silicon or Intel, works fully for 14 days without a card, then costs $9.99 a year for up to 3 Macs.

AgentAwake's safeguards
Default grace period before the Mac may sleep
2 min
Battery floor you choose, on battery power
10 to 25%
Minimum fan speed, lid closed, Balanced profile
40%
Watchdog: lid sleep back on if the app stops
60 s

Which method should you use?

Pick by where the Mac will be. At a desk with a monitor, clamshell mode needs nothing new. For one job you will check on, the two pmset commands are enough. If you close the lid and walk away often, use something that turns the setting back on by itself: the lid-awake script above, or AgentAwake if you also want the heat pause, fan control and a watchdog.

With the lid open, none of this is needed: caffeinate -i <command> keeps a long build or test run going until it ends.

Ways to keep an AI agent running with a MacBook lid closed
MethodLid closed on batteryTurns itself back onBattery and heat
caffeinate -iNoEnds with its commandNot involved
sudo pmset -a disablesleep 1YesNo, until you run disablesleep 0Nothing watches either
The lid-awake scriptYesWhen its command endsBattery floor; no heat check
Clamshell modeNo, needs power and an external displayNothing to undoNormal macOS behaviour
AgentAwakeYes, only while an agent works2 minutes after the work stops; watchdog within 60 sBattery floor, heat pause, fan control

Before you close the lid on an agent

  • pmset -g | grep SleepDisabled prints 1, or your tool says lid sleep is blocked
  • You know what turns lid sleep back on: a script's exit, an app, or you
  • On battery, something puts the Mac to sleep before the battery is empty
  • The Mac is on a hard surface, not in a bag or on a bed
  • The agent is not waiting for an approval it will never get while you are away
  • Unsaved work in other apps is saved, in case the battery runs out

Frequently asked questions

Does caffeinate keep a Mac awake with the lid closed?

Not reliably. caffeinate creates power assertions, and its man page describes them as stopping idle, display or disk sleep, or, with -s, system sleep on AC power only. Apple's documentation says the system may still sleep on lid close despite the idle-sleep assertion. To keep a MacBook running with the lid shut on battery, use sudo pmset -a disablesleep 1 or a tool that sets it.

How do I undo sudo pmset -a disablesleep 1?

Run sudo pmset -a disablesleep 0, then pmset -g | grep SleepDisabled: 0 means normal lid sleep is back. The setting stays until you change it, so undo it in the same script that set it rather than relying on memory.

Is it safe to close a MacBook lid while Claude Code is running?

Yes, if something keeps the Mac out of sleep and something watches battery and heat. A closed MacBook runs the same work as an open one; the risks are draining the battery and heat building up, worst in a bag where air cannot move. Leave it on a hard surface and set a battery floor.

Do I need an external display to run a MacBook with the lid closed?

Only for Apple's clamshell mode, which also needs power and a keyboard and mouse or trackpad. With pmset disablesleep 1, or a tool that sets it, a MacBook keeps running with the lid closed on battery and with no display attached.

Does this work for Codex, Gemini CLI, aider and opencode too?

Yes. pmset disablesleep works at the system level, so every process keeps running, whichever agent it is. AgentAwake recognises claude, codex, gemini, aider and opencode by default, takes start and stop reports from Codex and opencode through hooks, and lets you add other command names.

What does AgentAwake cost?

It works fully for 14 days without a card, then costs $9.99 a year for up to 3 Macs, paid inside the app through Stripe. It needs macOS 14 or later and runs on Apple silicon and Intel MacBooks.

Start with the problem

Want lid sleep to switch itself back on?

AgentAwake keeps your MacBook running with the lid closed only while Claude Code, Codex or another agent is working, then lets it sleep two minutes after the work stops. Battery floor, heat pause, fan control and a watchdog included. Free for 14 days, then $9.99 a year for up to 3 Macs.

Your business,in professional hands.