OmniscioGuidesAttach to a background session
Claude Code: attach to a background session
A background session keeps working after you close the terminal, so you need a way back into it. claude attach is that way: the same conversation in a terminal, with commands, keyboard shortcuts and features working as in any interactive session, apart from a few documented exceptions.
Last checked against Anthropic's documentation on November 26, 2026. By the Omniscio team. Updated .
What counts as a background session
Claude Code treats every session listed in agent view as a background session, whether or not a terminal is attached to it. You get one by starting a session with claude --bg, or by moving a running terminal session into the background.
A background session runs under a supervisor process, which is why it survives your terminal closing. Its conversation stays on disk, and the next time you attach or reply it resumes where it left off.
To see which kind of session you are in, run /status. The Session kind row reads background job · attached or background job · unattended in a background session, and interactive in any other.
Start one and get its ID
Launch a background session with a prompt, and Claude Code prints the session ID and the commands that manage it:
claude --bg "investigate the flaky login test"The short ID is also the session's directory name under ~/.claude/jobs/. A background session cannot be combined with -p, and Claude Code checks workspace trust for the directory before it starts.
To run a shell command as a background job instead of a Claude session, pair the flag with --exec:
claude --bg --exec 'pytest -x'Find the session you want
Run claude agents to open agent view, or claude agents --json to print the active sessions as a JSON array and exit. Add --all to include completed background sessions, and --cwd <path> to limit the list to sessions started under that directory.
Each entry carries the short id, a state such as working, blocked, done, failed or stopped, the process ID and status while the process is alive, and what it is waiting on when it is blocked.
The list is the supported way to read session state from a script, for example a status bar or a scheduler that supervises background work.
Attach to it
Pass the ID to claude attach:
claude attach 7c5dcf5dPart of a running session's name works in place of the ID, as in claude attach "auth refactor", and passing a name requires v2.1.290 or later. A name only works while the session's process is running, so pass the ID to open a stopped session.
When you attach, Claude posts a short recap of what happened while you were away. The session takes over the terminal as a full interactive session: commands, keyboard shortcuts and features work as usual, with two documented exceptions. A /ship or /steward watch does not start in a background session, and an attached session does not report its status to your terminal.
An attached session always renders in fullscreen mode, regardless of your terminal UI (tui) setting, because a background session has no terminal scrollback to append to. Scroll with PgUp, PgDn or the mouse wheel, and press Ctrl+O for transcript mode.
Detach without stopping it
- Left arrow on an empty prompt, or
/exit. Returns to agent view, whether you attached from there or ranclaude attachfrom your shell. Ctrl+Z. Also detaches, and always goes back to where you started.Ctrl+Ctwice on an empty prompt, orCtrl+Dtwice. A singleCtrl+Ckeeps its usual interrupt behaviour instead.
None of these stops the session: the left arrow, Ctrl+Z, /exit, and a double Ctrl+C or Ctrl+D all leave it running. To end it from inside, run /stop.
On Windows, a left arrow within about half a second of attaching shows Ambiguous ←, press again to detach, because the terminal can redeliver a press from before you attached. Press it again.
Read it without attaching
For a quick look, claude logs <id|name> prints the session's recent output. It accepts part of a running session's name the same way claude attach does.
For supervision, claude agents --json --all keeps listing sessions whose process has exited, so a scheduler can poll the state, status and waitingFor fields without opening a terminal for each one.
Stop, restart or remove it
| Command | What it does |
|---|---|
claude stop <id> | Stops the session. claude kill is accepted too. |
claude respawn <id> | Restarts a running or stopped session, for example to pick up an updated binary; a saved conversation resumes |
claude respawn --all | Restarts every running session, useful after an update |
claude rm <id> | Removes the session from the list and deletes a worktree Claude created for it when that is safe; the transcript stays for claude --resume |
A session that has finished, or is waiting for your next message, and stays unattached for about an hour has its process stopped to free resources. The conversation stays on disk, and attaching or replying resumes it. Pinning the session with Ctrl+T in agent view keeps its process running.
Attach versus resume
These are two different doors. claude attach opens a background session that is still in agent view. claude --resume restores a saved conversation from any session, and its picker shows background sessions marked bg.
Resuming a background session that is still running opens it in this terminal through claude attach, and a prompt you pass on the command line goes to it as its next turn.
Common mistakes
- Closing the terminal to stop a background session. The supervisor keeps it running; use
claude stopor/stop. - Attaching to a stopped session by name. A name works only while the process is running; pass the ID.
- Pressing
Ctrl+Conce to leave. It interrupts the turn first; press it twice on an empty prompt, or use the left arrow. - Combining
--bgwith-p. Claude Code refuses the combination. - Expecting the session to start without accepting workspace trust. Claude Code checks the directory's trust before it starts the background session.
- Expecting
claude --resumeto open a copy. On a still-running background session it attaches to that same session; only--fork-sessionstarts a copy.
How do I know it worked?
- The transcript opens with a recap of what happened while you were away.
/statusshowsSession kindasbackground job · attached.- After you detach,
claude agents --jsonstill lists the session, with its state.
Where these answers come from
- Anthropic's agent view page, manage sessions from the shell, read November 26, 2026: the commands, the ID and name rules,
--jsonand its fields, and the stopped-process behaviour. - Anthropic's agent view page, attach to a session, read November 26, 2026: the recap, fullscreen rendering, and every detach key.
- Anthropic's CLI reference, read November 26, 2026:
claude attach,logs,stop,respawn,rm, and--bg. - Anthropic's sessions page, resume a running background session, read November 26, 2026: how
--resumemeets a session that is still running.
Common questions
What does claude attach do?
It opens a background session in your current terminal as a full interactive Claude Code session. You give it the short ID printed when the session started with claude --bg, or part of the session's name while its process is running.
Does detaching stop the session?
No. The left arrow on an empty prompt, /exit, Ctrl+Z, and a double Ctrl+C or Ctrl+D all detach and leave the session running. To end it, run /stop inside it or claude stop from your shell.
How do I find a background session's ID?
It is printed when you start the session with claude --bg, and it is the session's directory name under ~/.claude/jobs/. You can also run claude agents --json to print the sessions and their IDs.
What is the difference from claude --resume?
claude attach opens a background session in agent view, running or stopped — pass the ID for a stopped one. claude --resume restores a saved conversation. Resuming a background session that is still running opens it through claude attach, which is the same session rather than a second copy.