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:

Three options compared

OptionKeeps running with the lid closed?Root / password needed?Best for
caffeinateNoNoRunning with the lid open
pmset disablesleep 1YesYesTemporary use, if you remember to undo it
Session-level lid-closed awake (HeyWakeup)YesNoEveryday 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:

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:

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

Download HeyWakeup · Pricing · Support

2026-12-01