在脚本和 CI 里用 `claude -p` 和 `codex exec`:非交互模式指南
怎么在脚本和 CI 里非交互运行 Claude Code(`claude -p`、`--bare`、`--allowedTools`)和 Codex(`codex exec`、`--sandbox`、`--json`),以及权限和安全注意项。


先说结论
想在 shell 脚本、定时任务或 CI 步骤里跑一次编程智能体而不打开交互界面,Claude Code 用 claude -p(打印模式),Codex 用 codex exec。两边都支持结构化 JSON 输出、显式权限边界和 API key 鉴权。
Claude Code (claude -p) | Codex (codex exec) | |
|---|---|---|
| 基本命令 | claude -p "Summarize auth.ts" | codex exec "Summarize auth.ts" |
| 写文件权限 | --allowedTools "Read,Edit,Bash" | --sandbox workspace-write |
| 结构化输出 | --output-format json / stream-json | --json (JSON Lines) |
| 跳过本机/项目配置 | --bare | --ignore-user-config / --ignore-rules |
| CI 鉴权 | ANTHROPIC_API_KEY | CODEX_API_KEY (inline) |
Claude Code 非交互模式(claude -p)
加上 -p(或 --print)会跑完后直接退出,成功返回状态码 0,失败返回非零。在脚本或未信任的目录里,建议加 --bare:启动更快,并跳过自动加载项目 hooks、MCP 服务、skills、插件和 CLAUDE.md。
# Fast, isolated one-off call (uses ANTHROPIC_API_KEY; skips OAuth/keychain and repo hooks)
ANTHROPIC_API_KEY="YOUR_KEY" claude --bare -p "Summarize README.md" --allowedTools "Read"
# Edit code with JSON output and a dollar cap
claude -p "Fix the failing unit test in auth.test.ts" --allowedTools "Read,Edit,Bash" --output-format json --max-budget-usd 1.50Codex 非交互模式(codex exec)
codex exec 把过程日志写到 stderr,只在 stdout 输出最终回复,方便直接重定向到文件或管道给下游工具;同时也支持通过 stdin 管道喂上下文、参数里写任务指令。
# Read-only summary piped to a file (default sandbox is read-only)
codex exec --ephemeral "Generate release notes for the last 10 commits" | tee release-notes.md
# Pipe data on stdin and format it
git diff main | codex exec "List any breaking API changes"
# Allow workspace edits (--full-auto is no longer supported) and stream JSONL events
CODEX_API_KEY="YOUR_KEY" codex exec --sandbox workspace-write --json "Fix lint errors" | jq--full-auto已不再支持:旧脚本请改成--sandbox workspace-write(或用--approve-for-me走自动审批)。--ephemeral不会把会话记录文件落盘,适合 CI 或批处理节点。--json向stdout输出 JSON Lines 事件流(thread.started、turn.started、item.*、turn.completed、turn.failed)。
既想用命令行驱动,又想保留界面与记录
纯 CI 流水线适合无界面跑一次就退;但平时定时巡检或批处理出了错,翻一屏纯文本日志很费劲。在 Mirasim 里,命令行驱动和可视化工作台可以同时要。
mirasim ui-cli:在脚本里操作运行中的 Mirasim——控制指令队列(queue steer / jump / resume)、回应确认、绑定模型、拉取本地模型,效果和界面操作一致。- 自动化与
/goal:给 11 个智能体任选其一配周期任务(可写入<workspace>/.mirasim/schedules.json),或发起持续推进的/goal,每次跑完的 diff 在电脑和手机上都能直接看。
第 24 章 · 查阅 › ui-cli 命令清单 → 文档第 22 章 `mirasim ui-cli` 完整命令清单。
常见问题
- 为什么
codex exec --full-auto跑不通了? - OpenAI 已经从
codex exec里移除了--full-auto。改成--sandbox workspace-write(需要自动审批时用--approve-for-me)。 - 用
claude --bare -p能走 Claude 订阅吗? - 不能。
--bare会跳过 OAuth 凭据和系统钥匙串;需要传ANTHROPIC_API_KEY、经--settings配apiKeyHelper,或走 Bedrock / Vertex / Foundry 凭据。 - 怎么在脚本里稳妥解析
claude -p或codex exec的输出? claude -p加--output-format json(或stream-json);codex exec加--json(JSON Lines 走stdout,进度日志走stderr)。
文章 · 在 GitHub Actions 里跑 Claude Code 和 Codex:配置、密钥与权限安全 → 在 GitHub Actions CI 工作流里跑 Claude Code 和 Codex。
文章 · 在 Claude Code 和 Codex 里用 API key:配置、自定义端点与切回订阅 → 给脚本和 CI 配 `ANTHROPIC_API_KEY` 与 `CODEX_API_KEY`。
文章 · 在 Claude Code 和 Codex 里跑定时任务:/loop、云端定时任务与自动化 → 给 Claude Code 和 Codex 配周期定时任务。
来源
以上按 2026 年 11 月 29 日 Anthropic 的非交互命令行文档(code.claude.com/docs/en/headless)和 OpenAI 的 Codex 非交互模式文档(learn.chatgpt.com/docs/non-interactive-mode)核对。最新参数以那两页为准。
接着读

OpenAI 与 Codex 报 403「unsupported_country_region_territory」排查指南
OpenAI API 与 Codex CLI 报 403 unsupported_country_region_territory 的原因、支持地区误判排查(VPN、云服务器 IP、IPv6),以及本地模型与其他可用智能体方案。

在 Amazon Bedrock、Vertex AI 和 Microsoft Foundry 上用 Claude Code 和 Codex
如何把 Claude Code 和 Codex 接到 Amazon Bedrock、Google Cloud Vertex AI 和 Microsoft Foundry(Azure):环境变量、config.toml 配置、认证方式、版本固定与限制对照。

用 Claude Code 连 Chrome 与 Codex 浏览器调试:控制台排错、Playwright 与实时预览
怎么用 Claude Code 连 Chrome 扩展读控制台和 DOM、用 Codex 桌面/云端浏览器或 Playwright 测网页,以及在 Mirasim 里直接预览正在运行的页面和产物。