MemoryLake
Back to all articles
TutorialNovember 13, 2026·11 min read

What Happens to Claude Code Background Tasks When You Exit? What /tasks Still Lists, Why Idle Tasks Can Be Stopped, and How to Keep Them Running (2026)

You asked Claude Code to start the dev server and run the full test suite in the background, then kept working on something else. An hour later you closed the terminal to switch projects. When you came back and started a new session, the server wasn't running, the test run was gone, and you weren't sure whether it had finished.

That's the documented behavior. Anthropic's interactive mode reference says: "Background tasks are automatically cleaned up when Claude Code exits."

It's also not the only way background work ends. Tasks can stop when their output gets too large, when your machine runs low on memory during an idle stretch, or when the subagent that started them finishes. And there's a way to keep them running that doesn't involve leaving a terminal open.

This guide covers what Claude Code background tasks are, how to start them, what /tasks shows, what happens when you exit, the other ways tasks stop, how to keep work running by backgrounding the session instead, how to turn background tasks off, and what to keep outside a task so the next session isn't starting blind.

What background tasks are

Claude Code can run a shell command without making you wait for it. The reference describes it: "When Claude Code runs a command in the background, it runs the command asynchronously and immediately returns a background task ID. Claude Code can respond to new prompts while the command continues executing in the background."

The usual candidates are things that take a while or never finish on their own. Anthropic lists build tools, package managers, test runners, development servers, and long-running processes.

Background work in a session isn't only shell commands. Subagents can also run in the background, and the /tasks view lists them alongside shells. Anthropic's overview of parallel work draws a line between the two: a background bash command "runs one shell command without blocking the conversation. It doesn't spawn an agent."

How a command ends up in the background

There are three ways:

  • Ask for it. Prompt Claude Code to run a command in the background.
  • Press Ctrl+B. It moves a regular Bash tool call to the background. "Tmux users must press Ctrl+B twice due to tmux's prefix key."
  • Let it time out. "When a command reaches its timeout before it finishes, Claude Code automatically moves it to the background instead of stopping it, unless the command starts with sleep."

Each background task gets an ID, and its output goes to a file. "Output is written to a file and Claude can retrieve it using the Read tool". That's how Claude can check on a test run it started earlier in the session.

What /tasks shows

/tasks is the view for background work in your current session. The commands reference describes it as: "View and manage background work in the current session, including subagents that have finished." It's also available as /bashes.

From the overview of parallel work: "For anything running in the background of the current session, /tasks lists each item and lets you check on, attach to, or stop it. The list also includes subagents that have finished."

Two details are easy to miss:

  • It runs immediately. Claude Code runs some commands without interrupting a response, and /tasks is one of them.
  • It covers the current session. It isn't a list of every background process on your machine, and it isn't a list of other sessions.

A recent change makes finished subagents easier to get back to. Claude Code v2.1.319, released November 6, added a row to /tasks that lists finished subagents, "so you can reopen one and message it even after it leaves the list or you restart".

The task list you toggle with Ctrl+T is a different thing. It's Claude's to-do checklist for a plan, not the background-task view. We covered it in our guide to why the Claude Code task list is empty.

What happens when you exit

The rule is short: "Background tasks are automatically cleaned up when Claude Code exits."

On macOS and Linux, cleanup reaches further than the shell. "When you stop a background task from /tasks or Claude Code stops it at exit, processes that detached from the task's shell, such as ones started under setsid or timeout, stop too".

So on macOS and Linux, a dev server started as a background task stops when the session ends, even if it was started in a way that would normally outlive the shell.

Exiting with subagents or workflows running. If you try to exit while some kinds of background work are running, such as subagents or workflows, Claude Code warns you. The agent view documentation says a dialog "warns that the work stops", and offers a choice to move the session to the background and exit instead. That option isn't shown when agent view is turned off.

Other ways background tasks stop

Exiting isn't the only end. The reference lists several more.

Too much output. "Background tasks are automatically terminated if output exceeds 5GB, with a note in stderr explaining why".

Memory pressure during idle time. "On macOS and Linux, Claude Code stops your running background tasks when the operating system reports critical memory pressure, provided the session has been idle for at least 30 minutes and no turn or subagent is running".

All of these have to hold: the system is under critical memory pressure, and the session has been idle for at least 30 minutes with nothing running. The debug log records why tasks were stopped, or why a pressure event left them running. To turn this behavior off, set CLAUDE_CODE_DISABLE_BG_SHELL_PRESSURE_REAP to 1. The environment variable reference notes that Windows has no memory-pressure signal.

Time limits in unattended sessions. "Background commands in a local session you work in from a terminal, the desktop app, or the VS Code extension have no time limit." In a session that runs unattended, such as a -p run or a cloud session, Claude Code stops a background command at its time limit.

The subagent that started it ends. "A background command that a foreground subagent started ends when that subagent's run ends, whether it finished, failed, or was interrupted".

That last one catches people who delegate setup work. If a subagent started your dev server in the background, the server's life is tied to that subagent's run.

How to keep background work running

If you want work to keep going after you leave, the documented route is to background the session, not to exit it.

The reference says: "If you background the session instead of exiting it, your background tasks keep running in the background session."

There are a few ways to do that:

  • /background, or /bg. "Detach the current session to run as a background agent and free this terminal." You can pass one more instruction, and you monitor the session with claude agents.
  • The exit dialog. When Claude Code warns that background work will stop, choose to move the session to the background and exit.
  • Start in the background. Passing --bg when you start Claude Code starts a session that goes straight to the background.

Backgrounded sessions show up in agent view, along with their state and whether they need your input. We covered what those sessions start with and share in our guide to what Claude Code's agent view is.

One caution for dynamic workflows. When you background a session with a workflow running, any of its subagents still running "start over and spend their tokens again" when Claude relaunches the workflow. The dialog tells you how many, and you can choose to stay until they finish.

Turning background tasks off

If you'd rather nothing ran in the background, there's a switch. "To turn off background commands, set the CLAUDE_CODE_DISABLE_BACKGROUND_TASKS environment variable to 1."

It has wider effects than the name suggests. "With it set, you also lose the Ctrl+B shortcut and other background features, such as running skills in the background." The environment variable reference adds that Claude Code then also stops automatically moving subagents, long MCP tool calls, and timed-out shell commands to the background.

Starting in bare mode has the same effect. And with background commands off, a command that hits its timeout stops instead of moving to the background.

What a background task doesn't keep

A background task produces output: logs, test results, a running server. Claude Code writes that output to a file, and Claude can read it during the session.

What the task doesn't produce is a summary of what the result meant. If a test run found a flaky test, or a build failed for a reason you then fixed, that conclusion lives in the conversation. The task is cleaned up at exit, and the reasoning is only as durable as the session it happened in.

That's fine for a dev server. It matters more for long runs whose results decide what to do next.

What people try first

Closing the terminal and expecting the server to keep running. Background tasks are cleaned up when Claude Code exits.

Looking for another session's tasks in /tasks. It shows background work in the current session.

Starting a process under setsid to outlive the session. On macOS and Linux, detached processes stop too when Claude Code stops the task.

Leaving a heavy machine idle. Under critical memory pressure, tasks can stop after 30 minutes of idle time.

Delegating a server start to a subagent. A foreground subagent's background command ends when the subagent's run ends.

The Fix: Keep background work alive when you need it

Step 1: Decide whether the work should outlive the session

Dev servers for a quick check can end with the session. Long test runs and builds you'll come back to shouldn't.

Step 2: Background the session instead of exiting

Use /background or choose to move to the background in the exit dialog, and check on it later with claude agents.

Step 3: Check /tasks before you leave

See what's still running, including finished subagents you may want to reopen.

Step 4: Write down what the results meant

Record the conclusions from long runs, such as which tests are flaky and why a build failed, outside the session.

Setting this up in MemoryLake

Background tasks are built to be temporary, and Claude Code cleans them up on exit. The conclusions you reach from their output, such as which test is flaky or which dependency broke the build, are what the next session needs.

MemoryLake is a memory layer for that kind of project context, such as decisions, findings, and constraints, kept in one place you maintain and available to the assistants you connect, such as ChatGPT, Claude Code, and Codex.

You write the entries yourself, in your own words. Nothing is read out of, written to, or deleted from your background tasks, their output files, your sessions, your session transcripts, your ~/.claude folder, or any vendor's store. Background tasks, /tasks, and cleanup work the way Anthropic describes them.

Step 1: Create an API key

Sign in and generate a key from the dashboard. Store it outside your repositories, like any other credential.

The MemoryLake console showing the API keys screen, where a new key is created and copied for use in an agent
The MemoryLake console showing the API keys screen, where a new key is created and copied for use in an agent

Step 2: Upload your first memories

Start with what you wrote in Step 4 of the fix: what long runs showed, which failures are known, and what you decided because of them. One finding per entry.

The MemoryLake workspace with the first documents uploaded, listing each file as it becomes searchable memory
The MemoryLake workspace with the first documents uploaded, listing each file as it becomes searchable memory

Step 3: Connect your AI & agents

Connect Claude Code and any other assistants you use. A new session, after the old one's tasks were cleaned up, can draw on the same findings.

The MemoryLake integrations screen listing the AI clients and agent frameworks that can be connected to the memory layer
The MemoryLake integrations screen listing the AI clients and agent frameworks that can be connected to the memory layer

What this changes in practice

The first difference is that closing a session doesn't close the investigation. The findings from yesterday's background runs are still written down.

The second is less re-running. When you know a test is flaky, you don't need to rediscover it in every session.

The third is continuity across tools. Codex or another assistant working on the same repository can use the same findings.

Best practices for Claude Code background tasks

Know the exit rule. Background tasks are cleaned up when Claude Code exits.

Background the session to keep work running. Use /background instead of exiting.

Use /tasks for the current session. It lists background work, including finished subagents.

Watch memory on long idle stretches. Tasks can stop under critical memory pressure after 30 idle minutes.

Keep servers out of subagents. A foreground subagent's background commands end with its run.

Turn background tasks off deliberately. The setting also removes Ctrl+B and other background features.

Write down what results meant. The task output is temporary, and the conclusions shouldn't be.

Conclusion

Claude Code background tasks let shell commands and subagents run while you keep working, and /tasks lists background work in the current session, including subagents that have finished. When Claude Code exits, background tasks are cleaned up, and on macOS and Linux that includes processes that detached from the task's shell.

Tasks can also stop when output exceeds 5GB, when the system reports critical memory pressure after 30 idle minutes, in unattended sessions at their time limit, or when the foreground subagent that started them ends. To keep work running, background the session instead of exiting, and keep the conclusions from long runs somewhere the next session can read.

Frequently asked questions

Do Claude Code background tasks keep running after I exit?

No. Anthropic says background tasks are automatically cleaned up when Claude Code exits. Background the session instead if you want them to keep running.

What does /tasks show in Claude Code?

Background work in the current session, including subagents that have finished. You can check on, attach to, or stop each item.

Why did my background task stop on its own?

Possible reasons include output over 5GB, critical memory pressure after 30 idle minutes on macOS and Linux, a time limit in an unattended session, or the end of the subagent that started it.

How do I keep a background task running?

Run /background to move the session to the background, or choose that option when the exit dialog warns that background work will stop.

How do I turn off background tasks?

Set CLAUDE_CODE_DISABLE_BACKGROUND_TASKS to 1. This also turns off the Ctrl+B shortcut and other background features.

What's the difference between /tasks and Ctrl+T?

/tasks shows background work such as shells and subagents. Ctrl+T toggles Claude's to-do checklist.

Can I reopen a finished subagent?

Yes. Since v2.1.319, /tasks lists finished subagents so you can reopen one and message it, even after a restart.