You give Claude Code a long task, close the lid, and come back to find it stopped halfway. Here is why that happens and three ways around it, from a one-line command to moving the agent onto a machine that never sleeps.
Claude Code is an ordinary process on your computer. When a laptop sleeps, the operating system freezes every process and drops the network. Claude Code can't call the model, run tests or write files until the machine wakes up, and a long turn may fail on the dropped connection. Nothing is wrong with Claude Code. Your computer simply stopped running it.
A second, separate problem: if you started Claude Code over SSH or in a terminal window you later close, the process ends with the terminal. The fixes below deal with both: keep the machine awake, and keep the process independent of any one terminal.
caffeinate
macOS ships with caffeinate, which holds a "don't sleep" assertion for as long as a
command runs. Wrap Claude Code in it:
caffeinate -i claude # no idle sleep while this Claude Code session runs
caffeinate -is claude # also block system sleep, but only while on AC power
caffeinate -i -w 12345 # keep awake until process 12345 exits
-i prevents idle sleep and -s prevents system sleep, which the man page
notes is only valid on AC power. This covers "I walked away and the Mac went to sleep". It is
not a reliable fix for closing a MacBook's lid, which is handled separately.
Apple's supported way to use a MacBook with the lid closed is closed-display mode: connect it to power, an external display, and an external keyboard and mouse or trackpad (Apple Support). The Mac stays awake with the lid shut. Third-party menu-bar apps such as Amphetamine offer keep-awake sessions, including a closed-display option; read their notes on heat and battery before you put a running laptop in a bag.
On Windows, open Control Panel → Power Options → "Choose what closing the lid does" and set
"Plugged in" to "Do nothing". On most Linux distributions with systemd, set
HandleLidSwitch=ignore (and HandleLidSwitchExternalPower=ignore) in
/etc/systemd/logind.conf, then reboot.
Keeping a laptop awake works, but it is fragile: a battery that runs down, a lost Wi-Fi network or an OS update still ends the session. For work that should run overnight, move it to a machine that is always on.
tmux runs your shell inside a session that doesn't belong to any terminal window, so closing the window or an SSH connection doesn't kill Claude Code. On its own, tmux doesn't stop a laptop from sleeping. It matters most on a remote machine (option 3), and Anthropic's own Remote Control docs recommend it for exactly that.
tmux new -s work # start a named session
claude # run Claude Code inside it
# Ctrl-b d # detach; Claude Code keeps running
tmux ls # list sessions
tmux attach -t work # reattach later, from any terminal or SSH login
With screen the equivalents are screen -S work, Ctrl-a d to
detach and screen -r work to reattach.
The robust fix is to let your laptop be just the screen. Claude Code runs on a machine that never sleeps, either a desktop at home or a small Linux server (VPS) from any cloud provider, and you connect over SSH. Anthropic lists Ubuntu 20.04+ or Debian 10+ and at least 4 GB of RAM as the system requirements, so size the server accordingly, plus whatever your project's build and tests need.
1. Connect and install the basics. On a fresh Ubuntu server:
ssh you@your-server
sudo apt update && sudo apt install -y git tmux
curl -fsSL https://claude.ai/install.sh | bash
2. Get your code onto it. Clone over SSH with a key added to your Git host (a per-repo deploy key keeps the blast radius small), or sign in with the GitHub CLI. Then install your project's dependencies as you would locally.
3. Sign in to Claude Code without a browser. Per Anthropic's authentication docs (September 2026) you have three options:
claude. If no browser opens, press
c to copy the login URL, open it on your laptop and sign in. When the browser shows a
login code instead of redirecting (common over SSH), paste it at the
Paste code here if prompted prompt. On Linux the login is stored in
~/.claude/.credentials.json.
claude setup-token.
It prints a one-year OAuth token tied to your Pro, Max, Team or Enterprise plan. On the server, set
export CLAUDE_CODE_OAUTH_TOKEN=... (in your shell profile, not in a repo). The docs note
this token can only make model requests, so it can't start Remote Control sessions.
ANTHROPIC_API_KEY to a key from the Claude Console
and you're billed per token instead of using your subscription.
4. Start the work inside tmux and walk away:
tmux new -s work
cd ~/code/myapp && claude
# give it the task, then Ctrl-b d and close your laptop
ssh you@your-server -t tmux attach -t work # check in later
For several tasks at once, give each one its own git worktree and tmux window. The setup is in
how to run multiple Claude Code sessions in parallel. The same
server works for Codex too (codex login --device-auth signs in without a browser); see
running Codex agents in parallel.
A little hygiene. Use SSH keys rather than passwords, run Claude Code as a normal user rather than root, and keep secrets out of the repository. If you'd rather not expose SSH to the internet at all, put the server and your devices on a private network such as Tailscale.
Once the agent runs somewhere that stays on, you can follow it from anywhere. Start it with
claude --remote-control inside tmux and it shows up in the Claude mobile app (this needs
a normal claude.ai login, not the setup-token token), or attach over SSH from a phone
terminal. The options are compared in
how to use Claude Code from your phone.
Maestro is a free desktop app for running coding agents in parallel, each in its own git worktree. It fits this setup in two ways:
ProxyJump or ProxyCommand) and runs the agent
there. Remote chat turns are started as detached processes on the server, so they keep going if you
close your laptop or quit Maestro, and the app catches up on what happened when it reconnects.
Either way you bring your own Claude (or Codex, Cursor, OpenCode, Kimi Code, Grok Build) account, and Maestro runs on macOS, Windows and Linux. More in Maestro as a Claude Code GUI.
caffeinate keep a MacBook running with the lid closed?Not reliably. It prevents idle sleep while a command runs. For lid-closed use, Apple's supported route is closed-display mode with power, an external display, and a keyboard and mouse.
The process resumes when the machine wakes, but a turn that was interrupted mid-request may
have failed. You can pick up a previous conversation with claude --continue or
claude --resume.
Yes: cloud sessions run on Anthropic-managed VMs and keep going after you close your laptop (as of September 2026). They clone your repo from GitHub, so they don't see local-only files, services or unpushed work.
Maestro is an independent project, not affiliated with Anthropic. Claude Code details were checked against Anthropic's public docs in September 2026.