Guide

Claude Code stops when your Mac sleeps

You gave Claude Code a long task, closed the lid, and came back to a dead session. Nothing is wrong with the agent. macOS put the machine to sleep, and sleep suspends every process, drops the network, and ends any SSH connection. This page covers the settings that stop that, in the order to try them, and the point where the honest fix is a different machine.

What actually happens at sleep

When a Mac sleeps, the CPU stops running your processes. The agent is not killed. It is frozen. When the machine wakes, the process continues. By then the API connection it was using has timed out. The tool call it was in the middle of has failed. On a laptop, a closed lid means sleep within seconds unless something prevents it. A lock screen does not cause sleep. A screensaver does not either. Only the sleep timer and the lid do.

Fix 1: caffeinate for a single task

macOS ships a tool that holds the machine awake while a command runs. Start the agent through it and the Mac stays up until the agent exits.

caffeinate -i claude

The flag keeps the system from idle sleeping. Add a second flag to keep the display on too, which matters only for tools that drive the screen.

caffeinate -di claude

This does not survive a closed laptop lid. The lid switch overrides every idle assertion. It also does nothing after the agent exits and you start the next task without it.

Fix 2: turn off sleep for the machine

On a desktop Mac, or a laptop on power, set the sleep timer to never and leave it. These are the settings we use on every agent Mac.

sudo pmset -a sleep 0
sudo pmset -a disksleep 0
sudo pmset -a displaysleep 10
sudo pmset -a womp 1

The display can still sleep. That saves power and changes nothing for a terminal process. The last line enables wake on network, which helps if something does put the machine down.

Fix 3: the closed lid

A MacBook with the lid closed sleeps unless it is on power and connected to an external display, keyboard, or mouse. That is Apple's clamshell rule. Without a display, one more setting forces the machine to stay awake with the lid shut.

sudo pmset -a disablesleep 1

Undo it later with the same command and a zero. Two warnings. The laptop will run warm in a bag or on a bed, so leave it on a hard surface. And it will drain a battery in a few hours if the power cable comes out, then die without a clean shutdown.

Fix 4: the terminal, not the machine

Sometimes the Mac stayed awake and the session still vanished. That is the terminal window closing or the SSH connection dropping, which sends a hangup to everything inside it. The answer is a terminal multiplexer. Run the agent inside a tmux session and it survives the window, the SSH drop, and the Wi-Fi hiccup.

brew install tmux
tmux new -s agent
claude

Detach with Ctrl-b then d. Reattach later from any terminal, or from your phone over SSH, with one command.

tmux attach -t agent

There is a full guide to this setup: keeping a Claude Code session alive with tmux.

Fix 5: come back after a reboot

Security updates restart the machine. Power blips restart it. After a restart nothing is running until someone logs in. For a machine that is meant to be unattended, enable automatic login for the agent user in System Settings. Then add a LaunchAgent that recreates the tmux session at login. The always-on Mac checklist has the full list, including battery backup.

When the laptop is the wrong machine

Everything above works. It also turns your laptop into a server you cannot take anywhere. Once the agent is doing real work overnight, most people move it to a machine whose only job is to stay on. There are four choices. A spare Mac mini at home. A Linux box, if you do not need Xcode. A Mac in a cloud. Or a hosted Mac mini that someone else keeps awake. The comparison of places to run a 24/7 agent has the costs side by side.

Frequently asked questions

Does caffeinate keep Claude Code running with the lid closed?

+

No. The lid switch overrides caffeinate on a MacBook. You need the machine on power with an external display connected, or the disablesleep power setting.

Will the lock screen or screensaver stop the agent?

+

No. Locking the screen and running the screensaver do not sleep the machine. Only the sleep timer, low battery, or a closed lid do. A locked screen is fine for a terminal agent.

Does this apply to Codex too?

+

Yes. Codex CLI is a terminal process like Claude Code. The same sleep settings, caffeinate, and tmux apply, and the same lid rule.

Can I keep the Mac awake from my phone?

+

You can start a task from your phone with Claude Code's Remote Control or over SSH, but you cannot stop a Mac from sleeping remotely once it decides to. Set the power settings once on the machine itself.

Related guides