How to keep Claude Code running after you close the terminal

Why closing the terminal or SSH kills Claude Code and Codex, and four fixes: tmux, headless mode, Hive, and an always-on machine. Plus how to resume.

You start Claude Code or Codex on a long task, close the laptop lid or the terminal window, and come back to find it gone. This article explains why that happens and walks through four ways to keep an agent running after the terminal closes, from the quick fix to a setup for several agents.

Why closing the terminal kills the agent

An interactive agent runs attached to a terminal. When that terminal goes away (you close the window or an SSH connection drops), the system sends a hangup signal to everything running in it, and the agent exits. Running it in the background with & or nohup doesn't help either: an interactive interface needs a terminal to draw on and read keys from.

So the fix is to give the agent a terminal that doesn't belong to your window: one owned by a background process that keeps running when you leave.

Option 1: tmux (or screen)

A terminal multiplexer runs a server that owns the terminals. Your window is just a view of them.

tmux new -s agent     # start a session
claude                # run the agent inside it
# Ctrl+b then d       # detach; the agent keeps running
tmux attach -t agent  # come back later, even over a new SSH connection

This is the classic answer and works everywhere tmux is installed. GNU screen does the same with screen -S agent and screen -r agent.

Option 2: run the agent headless

If the task needs no back-and-forth, skip the interactive interface. Claude Code's print mode runs one prompt and exits, which nohup can keep alive:

nohup claude -p "fix the failing tests in src/cart" > claude.log 2>&1 &

The catch: nobody is there to answer permission prompts, so the agent can only do what its permission settings already allow. That suits narrow, well-defined jobs, not exploratory work.

Option 3: Hive

Hive works like tmux underneath: a background daemon, hived, owns every agent's terminal. Detach with Ctrl+Space d, or just close the window, and the agents keep working. Run hive again to get them back.

hive                       # open Hive; type claude in the terminal
hive new codex -d -l api   # or start one in the background
hive list                  # what is running
hive attach 2              # open one

Where it differs from tmux is what it knows about the agents: the sidebar shows which one is working, which needs input and which is done, across every project. When you come back after an hour, you see at a glance which agent is waiting on you. It also reconnects on its own if the daemon restarts. See tmux or Hive for a side-by-side.

Option 4: an always-on machine over SSH

None of the above survives a reboot, and a sleeping laptop pauses everything on it. For agents that run overnight, put them on a machine that stays on (a desktop, a home server or a cloud VM), run tmux or Hive there, and connect over SSH. Dropped connections no longer matter: reconnect and reattach. If your network is flaky, mosh keeps the SSH side of things alive through roaming and sleep.

If the agent already died

The process is gone, but the conversation usually isn't. Claude Code can pick up where it left off with claude --continue (the most recent conversation in this directory) or claude --resume (choose one). Codex has codex resume. Changes the agent already made are on disk; check git status before you start it again.

Which to pick

  • One agent, already use tmux: tmux.
  • A narrow task with no prompts: headless mode.
  • Several agents, several projects: Hive, so you can tell which one needs you when you come back.
  • Overnight runs: any of these on an always-on machine.