Keep AI Agent Runs Alive with the Lid Closed (Claude Code, Codex)
AI agent runs can take hours, but closing the lid freezes the process and drops the network. Here's how to keep CLI and GUI agents running.
Why an agent stops when you close the lid
It isn't a CPU problem or a crashed agent — the system is asleep. macOS has separate lid-closed and idle sleep paths. The built-in "never sleep" setting only affects idle sleep, and caffeinate can only block idle sleep too. When you close the lid, the Mac enters clamshell sleep, the process is suspended, and network connections drop.
For an agent, the usual consequences are:
- the local process is frozen, and its state may be corrupted when you come back;
- SSH or screen sharing disconnects, so you can't keep watching;
- API requests time out, and retry logic may run the same step twice.
Three options compared
| Option | Keeps running with the lid closed? | Root / password needed? | Best for |
|---|---|---|---|
caffeinate | No | No | Running with the lid open |
pmset disablesleep 1 | Yes | Yes | Temporary use, if you remember to undo it |
| Session-level lid-closed awake (HeyWakeup) | Yes | No | Everyday agent runs |
The problem with pmset isn't that it doesn't work — it's the persistent change. Forget to undo it and the Mac keeps running hot in a bag; quitting or crashing won't restore the default. A session-level approach is active only while the app runs and restores the system default on quit, crash, or restart.
Start and stop by process
The most hands-off setup isn't "never sleep" — it's "stay awake only while the agent is running."
HeyWakeup's automatic conditions can watch processes: the defaults include claude, codex, cargo, and xcodebuild, and you can add your own process name. The app starts when the condition matches and lets go when the process exits. You can combine conditions with any or all.
A few practical setups:
- Claude Code / Codex: use a process condition; the app releases when the run ends.
- Local training / builds: watch
python,cargo,xcodebuild, and similar processes. - GUI agents such as Cursor: the GUI process is often always running, so a process condition may match all the time. A CPU-load or network-traffic condition is more reliable, or just start a manual scene.
Can you still check on it remotely?
Yes. While the lid-closed session is active, the system doesn't truly sleep and the network stays online, so SSH, screen sharing, and remote desktop keep working. You can close the lid, leave the Mac somewhere, and keep watching the agent from another device.
One caveat: a remote disconnect doesn't mean the task is done. A process condition is more accurate than "remote session active" — the latter only keeps the Mac awake while you're connected, and may let go too early when you disconnect.
Safety guards
The two things most likely to go wrong on a long run are battery and heat. HeyWakeup defaults:
- stop on low battery (20% by default, adjustable), and won't restart while the battery is low;
- thermal protection: pause and lock when the Mac runs hot, then resume after it cools down;
- automatic reset on quit, crash, or restart — no stuck no-sleep state.
For long runs, plug in and set a session limit; don't leave a heavily loaded Mac in a sealed bag.
FAQ
Does it detect Claude Code / Codex automatically? The default process list includes claude and codex. If your install uses a different process name, add it manually.
What about Cursor or other GUI agents? GUI processes are often always running. Use a CPU or network condition, or a manual scene, rather than a process name alone.
Does it need root or an admin password? No. Lid-closed awake is session-level and doesn't modify system settings.
Will it stay awake forever? You can set a session limit, low-battery protection, and thermal protection; the app lets go when the condition clears.
Will my remote connection drop? Not because of system sleep while the lid-closed session is active. Network problems are a separate matter.
Related reading
- Keep your Mac awake with the lid closed (no password)
- Use Your MacBook as a Music Player with the Lid Closed
- MacBook Clamshell Mode with an External Monitor: When Do You Need an App?
Download HeyWakeup · Pricing · Support