非交互模式
使用 Codex 在非交互模式下自动化常见工作流程。
codex exec "count the total number of lines of code in this project"在非交互模式下,Codex 不要求命令或编辑批准。默认情况下,它以 read-only 模式运行,因此无法编辑文件或运行需要网络访问的命令。
使用 codex exec --full-auto 允许文件编辑。使用 codex exec --sandbox danger-full-access 允许编辑和网络命令。
默认输出模式
默认情况下,Codex 将其活动流传输到 stderr,并仅将代理的最后消息写入 stdout。这使将 codex exec 管道传输到另一个工具而无需额外过滤变得更容易。
要将 codex exec 的输出写入文件,除了使用 shell 重定向(如 >)外,还有一个专用标志来指定输出文件:-o/--output-last-message。
JSON 输出模式
codex exec 支持 --json 模式,在代理运行时将事件作为 JSON Lines (JSONL) 流式传输到 stdout。
支持的事件类型:
thread.started- 当线程启动或恢复时。turn.started- 当轮转开始时。轮转包含用户消息和助手响应之间的所有事件。turn.completed- 轮转完成时;包括令牌使用情况。turn.failed- 当轮转失败时;包括错误详情。item.started/item.updated/item.completed- 当添加/更新/完成线程项时。error- 当流报告无法恢复的错误时;包括错误消息。
支持的项类型:
agent_message- 助手消息。reasoning- 助手思考的摘要。command_execution- 助手执行命令。file_change- 助手进行文件更改。mcp_tool_call- 助手调用 MCP 工具。web_search- 助手执行 web 搜索。todo_list- 计划工具活跃时的代理运行计划,在步骤更改时更新。
通常,agent_message 在轮转结束时被添加。
示例输出:
{"type":"thread.started","thread_id":"0199a213-81c0-7800-8aa1-bbab2a035a53"}
{"type":"turn.started"}
{"type":"item.completed","item":{"id":"item_0","type":"reasoning","text":"**Searching for README files**"}}
{"type":"item.started","item":{"id":"item_1","type":"command_execution","command":"bash -lc ls","aggregated_output":"","status":"in_progress"}}
{"type":"item.completed","item":{"id":"item_1","type":"command_execution","command":"bash -lc ls","aggregated_output":"2025-09-11\nAGENTS.md\nCHANGELOG.md\ncliff.toml\ncodex-cli\ncodex-rs\ndocs\nexamples\nflake.lock\nflake.nix\nLICENSE\nnode_modules\nNOTICE\npackage.json\npnpm-lock.yaml\npnpm-workspace.yaml\nPNPM.md\nREADME.md\nscripts\nsdk\ntmp\n","exit_code":0,"status":"completed"}}
{"type":"item.completed","item":{"id":"item_2","type":"reasoning","text":"**Checking repository root for README**"}}
{"type":"item.completed","item":{"id":"item_3","type":"agent_message","text":"Yep — there's a `README.md` in the repository root."}}
{"type":"turn.completed","usage":{"input_tokens":24763,"cached_input_tokens":24448,"output_tokens":122}}结构化输出
默认情况下,代理使用自然语言响应。使用 --output-schema 提供定义预期 JSON 输出的 JSON Schema。
JSON Schema 必须遵循严格模式规则。
示例 schema:
{
"type": "object",
"properties": {
"project_name": { "type": "string" },
"programming_languages": { "type": "array", "items": { "type": "string" } }
},
"required": ["project_name", "programming_languages"],
"additionalProperties": false
}codex exec "Extract details of the project" --output-schema ~/schema.json
...
{"project_name":"Codex CLI","programming_languages":["Rust","TypeScript","Shell"]}将 --output-schema 与 -o 结合以仅打印最终 JSON 输出。您也可以将文件路径传递到 -o 以将 JSON 输出保存到文件。
Git 存储库要求
Codex 需要 Git 存储库以避免破坏性更改。要禁用此检查,请使用 codex exec --skip-git-repo-check。
恢复非交互式会话
使用 codex exec resume <SESSION_ID> 或 codex exec resume --last 恢复以前的非交互式会话。这保留了会话上下文,以便您可以提出后续问题或给代理新的任务。
codex exec "Review the change, look for use-after-free issues"
codex exec resume --last "Fix use-after-free issues"只有对话上下文被保留;您仍然必须提供标志来自定义 Codex 行为。