Run Claude Code non-interactively in CI/CD pipelines, scripts, and scheduled tasks.
Claude Code supports headless (non-interactive) execution for use in automated pipelines, scripts, and scheduled workflows. Pass a prompt via flag or stdin and pipe the output to other tools.
Claude Code supports scheduled tasks stored in .claude/scheduled_tasks.json. Each task has a cron expression and a prompt:
[ { "id": "daily-dep-check", "cron": "0 9 * * 1-5", "prompt": "Check for outdated npm dependencies and open a PR if any are found.", "recurring": true }, { "id": "weekly-security-scan", "cron": "0 8 * * 1", "prompt": "Run a security audit on the codebase and report findings.", "recurring": true }]
The CronTask type used by the SDK daemon:
type CronTask = { id: string cron: string // Standard cron expression (5 fields) prompt: string // Prompt sent to Claude when the task fires createdAt: number // Unix timestamp (ms) recurring?: boolean // If false, task fires once and is deleted}
The watchScheduledTasks function is available for daemon architectures that own the scheduler externally:
import { watchScheduledTasks, query } from '@anthropic-ai/claude-code'const handle = watchScheduledTasks({ dir: '/path/to/project', signal: abortController.signal,})// handle.getNextFireTime() returns the epoch ms of the soonest fire, or nullconst nextFire = handle.getNextFireTime()for await (const event of handle.events()) { if (event.type === 'fire') { // Run the task for await (const msg of query({ prompt: event.task.prompt })) { console.log(msg) } } if (event.type === 'missed') { // One-shot tasks whose window passed while the daemon was down console.warn('Missed tasks:', event.tasks.map(t => t.id)) }}
watchScheduledTasks acquires a per-directory scheduler lock so a REPL session in the same directory won’t double-fire tasks. The lock is released when the abort signal fires.
Provide domain-specific instructions for automated runs without modifying project settings:
claude -p "Review the migration script" \ --system-prompt "You are reviewing database migration scripts. Flag any irreversible operations that lack a rollback strategy." \ --output-format text