#30: Background Tasks ⏳
Today we'll check out background tasks in Claude Code, which let long-running commands keep going while Claude gets on with the rest of our conversation.
Dev servers, watch builds, big test suites, and Docker builds can all tie up a session for minutes at a time. Sending them to the background frees Claude to keep reading, editing, and answering us while they run. Let's take a look.
Sending Work To The Background
The simplest way is to ask:
Start the dev server for Hal's dashboard in the background, then fix the failing login test.Claude starts the command as a background task and immediately gets a task ID back, so it can move straight on to the test.
If a command is already running in the foreground and taking longer than we'd like, pressing Ctrl+B moves it to the background. Inside tmux, it's Ctrl+B twice, since that's the tmux prefix key. The same shortcut works for commands we run ourselves in shell mode:
! bun run build --watchWhere The Output Goes
A background command writes its output to a file. When Claude wants to see how things are going, it reads that file with the Read tool, so it can check a build's progress or grab an error from a server log whenever it needs to.
We can ask for that directly too:
Check the dev server output for any errors since the last request.Checking On Them Ourselves
/tasks lists everything running in the background in this session, including subagents, and lets us stop them:
/tasksIt also answers to /bashes, and it runs right away even while Claude is in the middle of a response. When we stop a task from there, Claude moves on instead of waiting for it.
Automatic Backgrounding
Foreground commands run under a timeout, two minutes by default. When a command hits that timeout, Claude Code moves it to the background instead of killing it, and Claude sees a note like this:
Command did not complete within its 120s timeout and was moved to the backgroundThe note comes with the task ID and the path to the output file, so nothing is lost. Commands starting with sleep are the exception, and they stop at the timeout as before.
The gotcha here is the working directory. A cd inside a command that gets moved to the background doesn't carry over to later commands, and Claude Code says so in the result.
Watching With Monitor
Sometimes we want Claude to react the moment something happens, rather than check in later. That's the job of the Monitor tool. Claude writes a small script, runs it in the background, and gets each line of output as it arrives:
Tail logs/server.log and tell me as soon as a 500 error shows up.It's handy for tailing logs, polling a CI job until its status changes, or watching a folder for new files, and it can connect straight to a WebSocket feed too. Our conversation keeps going while it watches. Neat!
Each watch has a deadline, 5 minutes by default and 30 minutes at most. At the deadline Claude gets a notice, so it can start the watch again if it still needs it. Monitor commands also follow the same permission rules as Bash.
Watch Out
Background tasks are cleaned up when Claude Code exits. In a session we're working in from a terminal, the desktop app, or the VS Code extension, they have no time limit. In unattended runs, like claude -p, the Agent SDK, CI, or cloud sessions, they get a time limit of 30 minutes by default (10 minutes in a -p run with a plain text prompt), up to a maximum of 2 hours.
To turn background commands off entirely, there's an environment variable:
export CLAUDE_CODE_DISABLE_BACKGROUND_TASKS=1That also turns off Ctrl+B and the automatic move at timeout, so a command that hits its timeout just stops.
More info about background commands can be found at the Claude Code docs.