← Claude Code Hub
Mar 5, 2026 · 4 min read ✦ Tip #024

Claude Code Can Work While You Sleep

With the -p flag, Claude Code becomes a headless agent you can schedule with cron. Combined with --allowedTools, you get autonomous AI automation.

Claude Code isn't just an interactive assistant. With the -p (print) flag, it becomes a headless agent you can launch from scripts, CI/CD pipelines, or — here's where it gets interesting — from a cron job.

The difference from a regular script is that Claude doesn't execute fixed steps: it reasons about the current context and decides what to do. Combined with --allowedTools to control its permissions, you get a fully autonomous AI agent that works while you sleep.

TL;DR claude -p "prompt" --allowedTools "Read" "Bash(curl *)" turns Claude into an autonomous agent you can schedule with cron. It's not a static script — it's an agent that reasons and makes decisions. Headless is one of the four ways to send Claude Code to the background — the full map compares claude -p with Agent View, /loop and Routines, and tells you which one fits each case. Before reaching for system cron, check if a Desktop or Cloud scheduled task fits — the Cloud one (now called Routine) covers 90% of what you'd use cron for today. For the full picture of all 6 autonomous primitives, see the map.

Example — cron job that reviews staging logs every night:

# crontab -e
0 3 * * * cd /home/deploy/app && claude -p "Review logs/staging.log from the last 24h. \
  If you find errors, create a GitHub issue with the stack trace. \
  If clean, post a summary to Slack via curl." \
  --allowedTools "Read" "Bash(curl *)" "Bash(gh issue create *)" \
  --max-turns 10 \
  --max-budget-usd 0.50 \
  --output-format json >> /var/log/claude-review.log 2>&1

How it works

1. Basic usage

# Direct prompt
claude -p "explain this function"

# Stdin piping
cat error.log | claude -p "explain these errors and suggest fixes"

# Continue a previous session
claude -c -p "check for type errors in the last changes"

The -p flag disables the interactive interface. Claude processes the prompt, executes the necessary actions, and returns the result to stdout.

2. Control permissions

# Read-only — risk-free analysis
claude -p "audit this codebase" --allowedTools "Read" "Glob" "Grep"

# Read + HTTP — can notify but not modify
claude -p "check health" --allowedTools "Read" "Bash(curl *)"

# Everything allowed (be careful)
claude -p "fix all lint errors" --dangerously-skip-permissions

--allowedTools is the key to safe automation. It defines exactly which tools Claude can use, with pattern matching for specific commands.

3. Limit cost and execution

claude -p "complex analysis" \
  --max-turns 15 \
  --max-budget-usd 1.00

--max-turns limits how many actions Claude can take. --max-budget-usd caps spending. Both are essential for unattended execution. Anthropic announced that from June 15, 2026 that spend would move to a separate monthly Agent SDK credit, but it cancelled the change; for now it still comes out of your subscription limits.

4. Output format for scripts

# JSON for programmatic parsing
claude -p "list all TODO comments" --output-format json

# Streaming JSON for real-time processing
claude -p "analyze" --output-format stream-json

5. Print a TUI report as plain text

There is one use of the same flag that has nothing to do with automation: several commands that open a screen inside a session will print plain text instead when you run them with -p, ready to paste somewhere or pipe through grep.

claude -p "/usage"          # both consumption windows at once
claude -p "/skill-doctor"   # the skill table with its cost and its uses

The first one splits your consumption by skill and by MCP server; the second flags the skills you have never invoked.

Reference

Flag What it does Example
-p / --print Enables headless mode claude -p "query"
--allowedTools Permitted tools --allowedTools "Read" "Bash(curl *)"
--max-turns Action limit --max-turns 10
--max-budget-usd Spending cap --max-budget-usd 0.50
--output-format Output format --output-format json
--dangerously-skip-permissions Skip all permissions With caution, and almost never the option you want

Official docs: Run Claude Code programmatically

Related: Computer use — when the terminal is not enough · Notify when Claude finishes · Map of the 4 ways to send Claude to background · The Agent SDK credit that was announced and cancelled

If permission prompts are what slows your interactive sessions, this guide lays out the five ways to cut them without losing control.

Found this useful? See more of wmedia.es in your Google results.

Add wmedia.es as a preferred source on Google
FREE GUIDE

You don't need Opus every time.

Model and effort are two settings, not one. 8 pages with the settings that actually move your bill, a three-question diagnostic for when something fails, and the price ladder.

  • 1. What "better" means capability, time and money
  • 2. Model who you send
  • 3. Effort how much rope you give
  • 4. Diagnostic three questions when something fails
  • 5. The bill where it comes from
Are you a professional Web developer?

You'll receive the guide by email · Unsubscribe anytime

of 8
#

Wmedia · 8 pages
FREE GUIDE · 8 pages · 2 dials

You don't need Opus every time.

Model and effort are two settings, not one. 8 pages with the settings that actually move your bill, a three-question diagnostic for when something fails, and the price ladder.

Are you a professional Web developer? · Unsubscribe anytime

Using Opus for everything? See when you don't need it.