Long-running tasks (test suites, builds, training runs) used to block your terminal. Claude Code's background tasks and the Monitor primitive let you start the work and keep going. Claude is notified when it finishes.
Learning Objectives
After this lesson, you will be able to:
Run long commands in background with `run_in_background: true` so your session doesn't block
Use `Monitor` to stream events from a background process — line-by-line notifications without polling
Schedule deferred work via `ScheduleWakeup` (dynamic /loop) and `CronCreate` (cron-style scheduling)
Pick the right background pattern — fire-and-forget, monitor stream, scheduled wake-up, persistent cron
When you want line-by-line awareness of a running process (not just "did it finish"):
pythonrunnable cell
1
2
3
4
5
6
7
8
9
10
11
# Start a long process
proc = Bash(
command="pytest tests/ -v",
run_in_background=True
)
# Monitor its output
Monitor(
process_id=proc.id,
pattern="FAILED|ERROR" # only notify on these lines
)
You get a notification each time the pattern matches. Useful for:
Watching a CI deploy log for failure markers
Tailing a server log for error events
Watching a training run for loss anomalies
Polling-style "wait until X happens" without burning tokens
# Wait until a service is healthy, no polling tokens spent on each check
Monitor(
command="until curl -sf localhost:3000/health; do sleep 2; done",
description="Wait for dev server to be ready"
)
Claude is notified once the server starts responding. Single notification, no token cost during the wait.
For autonomous agents that need to fire on a schedule:
pythonrunnable cell
1
2
3
4
5
CronCreate(
schedule="0 9 * * 1", # Monday 9am
prompt="Audit npm dependencies for new CVEs. File Linear tickets for any CRITICAL.",
reason="Weekly security audit"
)
Cron-formatted schedule strings:
0 * * * * — every hour
0 9 * * * — daily at 9am
0 9 * * 1 — Monday at 9am
0 0 1 * * — first of every month
Manage with CronList, CronDelete.
Use cases
Weekly dep audits → file tickets for vulnerabilities
Daily error-log triage → summarize Sentry events into a channel
1. Start build in background (run_in_background)
2. Continue editing files in main session
3. When build notification arrives, react (deploy if pass, fix if fail)
CronCreate(
schedule="0 8 * * 1-5", # weekdays at 8am
prompt="""
1. Run /ultrareview on the develop branch since last Friday
2. Summarize critical findings
3. Post to #engineering Slack via slack MCP
""",
reason="Daily branch quality check"
)
ScheduleWakeup(
delaySeconds=900, # 15 min — past cache window, but right for "settle then verify"
reason="Verify post-deploy metrics settled",
prompt="Query Grafana for error rate over last 15 min; compare to pre-deploy baseline; alert if elevated"
)
# Cron job that runs Claude in headless mode for a scheduled task
0 8 * * 1-5 claude --headless --permission-mode dontAsk \
--prompt "Run /ultrareview on develop since Friday. Post results to Slack."