C.W.K.
Stream
Lesson 09 of 11 · published

Scripting with codex exec

~14 min · codex, exec, scripting, automation

Level 0🌱 Novice
0 XP0/70 lessons0/11 achievements
0/120 XP to next level120 XP to go0% complete

codex exec — the print-mode of Codex

codex exec is the non-interactive entry point. Prompt in (args or stdin), output to stdout, exit. The same mode every CLI ships under a different name; in Codex it's exec.

Output flags: --output-last-message (just the final answer, ideal for piping), --output-format text|json|markdown. Tool restriction: Codex inherits the active sandbox + approval mode, so a CI invocation should pin both via --sandbox and --ask-for-approval.

Code

Three real exec patterns·bash
# 1. Generate a commit message from staged diff
git diff --staged | codex exec \
  --ask-for-approval never --sandbox read-only \
  "Write a concise conventional-commit message" \
  --output-last-message

# 2. Triage failing tests
npm test 2>&1 | tail -100 | codex exec \
  --sandbox read-only \
  "Diagnose these failures and rank by likely root cause" \
  > triage.md

# 3. Generate OpenAPI from route handlers
cat src/routes/*.ts | codex exec \
  --sandbox read-only \
  "Generate OpenAPI 3.1 spec for these handlers" \
  > openapi.yaml
Cron job example·bash
# crontab -e
30 22 * * 1-5 cd /workspace && /usr/bin/env codex exec \
  --ask-for-approval never \
  --sandbox workspace-write \
  "Update dependencies, run tests, write a one-paragraph
   summary to /workspace/nightly-summary-$(date +%F).md" \
  --output-last-message \
  >> /var/log/codex-nightly.log 2>&1

External links

Exercise

Replace one shell script you currently run by hand with a codex exec invocation. Pin sandbox + approval explicitly. Schedule it in cron. After a week, audit the logs to verify it ran cleanly.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.