Five ways to run Claude Code in the background
They differ in what has to stay running, and who starts the work.
| Starts with | Keeps running while | Good for | |
|---|---|---|---|
| Background tasks | Ctrl+B, or Claude runs a command in the background | The session is open | Long builds and tests, subagents, inside one conversation |
| Agent view | claude --bg "<prompt>" | Your computer is on (the terminal can close) | Several sessions you start now and check later |
| Headless mode | claude -p "<prompt>", from a script or cron | The command runs | Scripts, cron jobs, CI |
| Cloud sessions | claude --cloud "<task>" | Anthropic's servers run it (laptop can close) | Long tasks on a GitHub repo |
| Scheduled tasks | /schedule, the desktop app or /loop | Depends on the kind: cloud, app open, or session open | Recurring work; see how to make Claude Code proactive |
Background tasks inside a session
Press Ctrl+B while Claude is running a Bash command or a subagent to send it to the background (twice in tmux), and keep talking to Claude. Claude can also start a command in the background on its own. Subagents Claude spawns run in the background by default.
Type /tasks to list running shells and subagents and check on them. In a local terminal session a background command has no time limit, but it stops when Claude Code exits. To run several agents side by side, see how to run multiple Claude Code agents.
Agent view: sessions that outlive the terminal
claude --bg "<prompt>" starts a session in the background and returns right away; /bg moves the session you're in to the background. A supervisor process runs them, so closing the terminal doesn't stop them, but shutting down or restarting the computer does, and an unattached idle session is stopped after about an hour.
claude agents opens the agent view; claude attach <id>, claude logs <id> and claude stop <id> manage one session. Agent view is a research preview, so the commands may change.
Run Claude Code headless with claude -p
Run one prompt and exit
-p(or--print) runs Claude Code without the interactive interface and exits when it's done, with exit code 0 on success. You can also pipe input in.claude -p "List the failing tests and why they fail" cat error.log | claude -p "Explain this error"Get output a script can read
--output-format jsonreturns the result with the session ID and metadata, includingtotal_cost_usd, an estimate computed on your machine.stream-json(with--verbose) streams events as they happen.claude -p "Summarize this repo" --output-format json | jq -r '.result'Allow only the tools it needs
In headless mode nobody is there to approve a prompt. Since v2.1.302 a run with no mode set starts in Manual and denies anything that would need approval. Use
--allowedToolsfor what it may do and--permission-mode dontAskto deny the rest instead of waiting.claude -p "Run the test suite and summarize failures" \ --permission-mode dontAsk --allowedTools "Read,Bash(npm test)"Cap the run
--max-turnsstops after a number of agent turns, and--max-budget-usdat an estimated cost (it can overshoot a little). Both work only with-p.claude -p "Fix the lint errors" --max-turns 15 --max-budget-usd 3Continue the same conversation later
Save the session ID from the JSON output and pass it to
--resume, or use--continuefor the most recent session in the directory.id=$(claude -p "Start a code review of src/" --output-format json | jq -r '.session_id') claude -p "Now check the tests too" --resume "$id"Run it on a schedule with cron
Anthropic's docs don't include a cron example; this one uses only documented flags. Cron has a minimal PATH, so use the full path from
which claude, and create the log folder first. Don't add--barehere: it skips your subscription login and needs an API key.# crontab -e: weekdays at 9:00 0 9 * * 1-5 cd /path/to/repo && /path/to/claude -p "Summarize yesterday's commits and list new TODO comments. Do not edit files." --permission-mode dontAsk --allowedTools "Read,Bash(git log *),Bash(git diff *)" --max-turns 10 --output-format json >> "$HOME/logs/claude-daily.log" 2>&1
Permissions for unattended runs
dontAskplus an--allowedToolslist is the locked-down choice: it denies anything not on the list.autolets a classifier review each action instead of you. Anthropic notes it doesn't guarantee safety.acceptEditsapproves file edits and simple file commands, but not other shell commands.bypassPermissionsskips prompts and safety checks. Anthropic says to use it only in isolated containers or VMs without internet access, as it offers no protection against prompt injection.- For rules that apply to every run in a project, put
permissions.allowandpermissions.denyin.claude/settings.json. Deny rules always win.
Cloud options: when your computer is off
- Cloud sessions:
claude --cloud "<task>"runs on Anthropic's infrastructure from your GitHub branch and keeps going with the laptop closed. Send follow-ups withclaude -p "..." --cloud <session-id>. - Cloud scheduled tasks: created with
/scheduleor at claude.ai/code, they run on a schedule (at most hourly), from an API call or from GitHub events. Pro, Max, Ultra, Team and Enterprise; research preview. - GitHub Actions:
anthropics/claude-code-action@v1with ascheduletrigger. An OAuth token fromclaude setup-tokenuses your subscription; an API key is billed per token.
Give a background agent a standing job
Background tasks, agent view and cron all start work you've already decided on. Crewmark gives a Claude Code session an ongoing role instead: it wakes on a heartbeat during working hours, takes the next issue from a board and messages you when it's blocked.
Turn a session into an employee
Pick a role and a goal in numbers, and run the setup command Crewmark gives you in Claude Code. That conversation becomes the employee.
Let the heartbeat start the work
The Crewmark daemon wakes the session every 15, 30 or 60 minutes during its working hours, on the subscription you already pay for, and leaves it alone outside them.
Assign issues, answer messages
Add work as issues on the board; the employee picks them up, comments as it goes and asks you when it needs a review. See what an AI employee is.
Claude Code background agent FAQ
How do I run Claude Code in the background?
Inside a session, press Ctrl+B to background a running command or subagent. To start a whole session in the background, use claude --bg "<prompt>". For scripts and cron, use headless mode: claude -p "<prompt>".
What is Claude Code headless mode?
Running Claude Code with -p (or --print): it takes a prompt, works without the interactive interface and exits, printing the result as text, JSON or a stream of JSON events.
Does a background agent keep running when I close the terminal?
Background tasks inside a session stop when Claude Code exits. Sessions started with --bg keep running after the terminal closes, until the computer shuts down. Cloud sessions and cloud scheduled tasks keep running with the computer off.
Which permission mode should an unattended run use?
Start with --permission-mode dontAsk and an --allowedTools list of exactly what the job needs. Use bypassPermissions only inside an isolated container or VM without internet access.
Does claude -p use my Claude subscription?
It uses whatever you're logged in with, unless you pass --bare, which skips the login and needs an API key. Anthropic's docs don't spell out how headless runs count toward a plan's usage limits.
How do I check on background agents?
Use /tasks inside a session, claude agents for sessions started with --bg, and the log file or JSON output for headless runs.
Checked against Anthropic's Claude Code documentation on November 2, 2026.