Codex: Codex Non-Interactive Mode & Cross-Platform

Last updated: 2026-08-31

Non-interactive mode makes Codex suitable for scripts and automation, while Windows and Worktrees require special configuration.

📋 Prerequisites: Understanding Codex CLI basic operations

1. What You Will Learn


2. Non-Interactive Mode

(1) Launch Methods

BASH
# Pass task directly, exits after execution
codex "Fix all lint errors"

# Quiet mode, less output
codex --quiet "Generate API documentation" > api-docs.md

# JSON output, suitable for programmatic parsing
codex --json "List all TODOs" > todos.json

# Fully automated
codex --full-auto --approval-policy approve "Run all tests and fix failures"

(2) Pipe Input

BASH
# Read from stdin
echo "Explain this code" | codex --quiet

# Read from file
cat error.log | codex --quiet "Analyze the error root cause"

# Combine with git
git diff main | codex --quiet "Review the changes"

(3) Exit Codes

Exit Code Description
0 Task completed successfully
1 Task failed
2 Parameter error
130 User interrupted (Ctrl+C)

▶ Example 1: Alice's Automation Script

BASH
#!/bin/bash
# auto-fix.sh - Daily auto-fix script

# Pull latest code
git pull origin main

# Fix lint
codex --full-auto --quiet "Fix all lint errors"

# Generate missing tests
codex --full-auto --quiet "Generate unit tests for source files without tests"

# Run tests
npm test
if [ $? -eq 0 ]; then
  git add -A
  git commit -m "chore: daily auto-fix"
  git push origin main
  echo "✅ Auto-fix completed and pushed"
else
  echo "❌ Tests failed, skipping push"
fi

3. Windows Environment

(1) Installation Methods

POWERSHELL
# Option 1: npm (requires Node.js)
npm install -g @openai/codex

# Option 2: WSL (recommended)
wsl
# Install as Linux within WSL

(2) PowerShell Configuration

POWERSHELL
# Set API Key
$env:OPENAI_API_KEY = "sk-your-key"

# Permanent configuration
[System.Environment]::SetEnvironmentVariable("OPENAI_API_KEY", "sk-your-key", "User")

# Set Base URL (DeepSeek)
$env:OPENAI_BASE_URL = "https://api.deepseek.com"

(3) Common Issues

Issue Solution
Different path separators Use path.join() or forward slashes
Encoding issues Set $OutputEncoding = [System.Text.Encoding]::UTF8
Permission restricted Run PowerShell as Administrator
Command not found Check PATH environment variable
BASH
# Install and use Codex within WSL
sudo npm install -g @openai/codex
export OPENAI_API_KEY="sk-xxx"
codex

4. Git Worktrees Integration

(1) What Are Worktrees

Git Worktrees let you check out multiple branches of the same repository into different directories simultaneously, ideal for parallel development.

BASH
# Create worktree
git worktree add ../feature-auth feature/auth
git worktree add ../feature-payment feature/payment

(2) Codex + Worktrees

Each Worktree can run an independent Codex instance:

BASH
# Terminal 1: Work on auth feature
cd ../feature-auth
codex "Implement JWT authentication"

# Terminal 2: Work on payment feature
cd ../feature-payment
codex "Add Stripe payment"

▶ Example 2: Bob's Parallel Development

BASH
# Bob uses Worktrees for parallel development
git worktree add ../fix-bug-123 fix/bug-123
git worktree add ../new-feature new/feature-x

# Terminal 1
cd ../fix-bug-123
codex --full-auto "Fix Issue #123"

# Terminal 2
cd ../new-feature
codex "Implement feature X"

# Two tasks execute in parallel without interference

(3) Worktrees Cleanup

BASH
# List all worktrees
git worktree list

# Remove worktree
git worktree remove ../feature-auth

# Prune stale worktrees
git worktree prune

5. Cross-Platform Considerations

Consideration macOS/Linux Windows
Path Separator / \ (recommend /)
Line Endings LF CRLF
Environment Variables export KEY=value $env:KEY = "value"
Shell bash/zsh PowerShell/WSL
Permissions chmod Properties dialog

❓ FAQ

Q Can non-interactive mode use Computer Use?
A No. Computer Use requires a graphical interface and interactive confirmation, which non-interactive mode doesn't support.
Q Windows native or WSL — which is better?
A WSL is better. Codex CLI has the best compatibility on Linux; WSL provides a native Linux experience.
Q Will Codex instances in Worktrees conflict?
A No. Each Worktree is an independent directory, Codex instances don't interfere. But don't modify the same file on the same branch in two instances.
Q How does non-interactive mode handle confirmation requests?
A Requires --approval-policy approve or --full-auto. Otherwise it will hang. Read-only operations should use deny; modifications need approve.
Q What about encoding issues on Windows?
A Set PowerShell encoding to UTF-8: [Console]::OutputEncoding = [System.Text.Encoding]::UTF8.

📖 Summary


📝 Exercises

  1. Basic (⭐): Use non-interactive mode for a code review, output to a file.
  2. Intermediate (⭐⭐): Write an automation script using non-interactive mode for lint fix + test + commit.
  3. Advanced (⭐⭐⭐): Set up a parallel development environment with Git Worktrees, two Codex instances working on different branches simultaneously.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏