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.
That covers the quick fixes. The full guide also covers the closed lid, keeping the terminal session alive with tmux, coming back after a reboot, and when a laptop is the wrong machine for an always-on agent: read the full guide on macrun.dev.
Full disclosure, I run MacRun, which rents dedicated Mac minis for builds and coding agents.
About the author
- MacRun rents dedicated M6 Mac minis by the day, week or month. Each Mac belongs to one customer. People use them to build and ship iOS apps from Windows or Linux, as CI runners, and as always-on Macs for Claude Code and Codex. Guides here are from macrun.dev.
Comments
Join the discussionOne thing worth adding is that keeping the machine awake solves only the infrastructure side of the problem. It doesn’t guarantee the agent task survives a dropped API connection, terminal crash, or unexpected reboot.
For longer coding tasks, I’d treat caffeinate and tmux as convenience measures, but make the workflow resumable as well: persist task state, keep important outputs in files or commits, and make tool operations safe to retry. That way, even if the Mac does sleep, the work doesn’t have to start from zero. An always-on machine improves availability; recoverability is what makes the setup reliable.
Good point, and I agree. Staying awake only keeps the Mac alive. It does not save the work.
A few things that help with Claude Code in particular:
claude --continuepicks up the last session in that folder, andclaude --resumelets you choose an older one. A crashed terminal does not have to end the task.- Ask the agent to commit after each working step, on its own branch. A bad reboot then costs one step, not the whole run.
- Keep a short progress file in the repo with what is done and what is next. After a restart, the agent reads it and carries on.
- On a Mac that runs unattended, set
sudo pmset -a autorestart 1so it comes back on by itself after a power cut.
Thanks for adding this. Recoverability is the part most setups skip.
