Codex non-interactive mode
Non-interactive mode allows you to use Codex in scripts, CI/CD pipelines, or headless environments. This section details how to configure and use non-interactive mode.
Basic Usage
Execute a single task
Basic exec command
# Execute simple tasks
codex exec "List Python files in the current directory"
# Use double quotes for complex commands
codex exec "Analyze the code structure of the src/ directory"
codex exec "List Python files in the current directory"
# Use double quotes for complex commands
codex exec "Analyze the code structure of the src/ directory"
Read from standard input
stdin input
# Read from pipe
echo "Explain this code" | codex exec --stdin
# Read from file
codex exec --stdin < task.txt
# Multi-line input
cat <<EOF | codex exec --stdin
Create a function to calculate the factorial
Test this function
EOF
echo "Explain this code" | codex exec --stdin
# Read from file
codex exec --stdin < task.txt
# Multi-line input
cat <<EOF | codex exec --stdin
Create a function to calculate the factorial
Test this function
EOF
Non-interactive mode is ideal for automated tasks and script integration.
Command-line arguments
Common Parameters
| Parameter | Description |
|---|---|
exec "任务" | Execute the specified task |
--stdin | Reading tasks from standard input |
--model, -m | Specify model |
--no-auto-approve | Disable automatic confirmation |
--output, -o | Output to file |
Complete example
Complete parameter example
# Specify model
codex exec -m gpt-5.4-mini "Review code"
# Output to file
codex exec "Create README" -o README.md
# Disable auto-confirmation
codex exec --no-auto-approve "Run tests"
# Combined use
codex exec -m gpt-5.4 "Review PR" --output review.md
codex exec -m gpt-5.4-mini "Review code"
# Output to file
codex exec "Create README" -o README.md
# Disable auto-confirmation
codex exec --no-auto-approve "Run tests"
# Combined use
codex exec -m gpt-5.4 "Review PR" --output review.md
Configuration
Configuration file settings
Configure non-interactive mode
# Non-interactive mode default settings
[exec]
# Default model
default_model = "gpt-5.4"
# Auto-approval threshold
auto_approve_threshold = "medium"
# Timeout (seconds)
timeout = 300
[exec]
# Default model
default_model = "gpt-5.4"
# Auto-approval threshold
auto_approve_threshold = "medium"
# Timeout (seconds)
timeout = 300
Execution strategy
In non-interactive mode, the default policy is usuallydeny(rejecting operations that may have side effects):
Set execution policy
# Use ask strategy
codex exec --approval ask "Create file"
# Use approve strategy (use with caution)
codex exec --approval approve "Run tests"
# Use deny strategy (read-only)
codex exec --approval deny "Analyze code"
codex exec --approval ask "Create file"
# Use approve strategy (use with caution)
codex exec --approval approve "Run tests"
# Use deny strategy (read-only)
codex exec --approval deny "Analyze code"
For security reasons, operations that may have side effects are disabled by default in non-interactive mode.
Shell script integration
Basic script
Shell script example
#!/bin/bash
# Using Codex for Code Review
codex exec Review code in the src/ directory --output review.md
if [ $? -eq 0 ]; then
echo Review complete, results saved to review.md
else
echo Review failed
fi
# Using Codex for Code Review
codex exec Review code in the src/ directory --output review.md
if [ $? -eq 0 ]; then
echo Review complete, results saved to review.md
else
echo Review failed
fi
Batch processing
Batch processing script
#!/bin/bash
# Batch review multiple files
for file in $(find src -name "*.py"); do
echo Review:$file"
codex exec Review$fileFile --output "review_$(basename $file).md"
done
# Batch review multiple files
for file in $(find src -name "*.py"); do
echo Review:$file"
codex exec Review$fileFile --output "review_$(basename $file).md"
done
Conditional execution
Conditional execution
#!/bin/bash
# Decide whether to use Codex based on the number of files
FILE_COUNT=$(find src -name "*.py" | wc -l)
if [ $FILE_COUNT -gt 10 ]; then
echo There are many files, use Codex for batch review
codex exec Review all Python files --output full_review.md
else
echo There are few files, review them one by one
for file in $(find src -name "*.py"); do
codex exec Simple review$file"
done
fi
# Decide whether to use Codex based on the number of files
FILE_COUNT=$(find src -name "*.py" | wc -l)
if [ $FILE_COUNT -gt 10 ]; then
echo There are many files, use Codex for batch review
codex exec Review all Python files --output full_review.md
else
echo There are few files, review them one by one
for file in $(find src -name "*.py"); do
codex exec Simple review$file"
done
fi
CI/CD integration
GitHub Actions
GitHub Actions workflow
span style="color: green;">
name: Code Review
on: [pull_request]
jobs:
codex-review:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Codex
uses: openai/codex-action@v1
- name: Run Code Review
run: |
codex exec "Review code changes in this PR" \
--output review.md
- name: Upload Review
uses: actions/upload-artifact@v4
with:
name: code-review
path: review.md
name: Code Review
on: [pull_request]
jobs:
codex-review:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Codex
uses: openai/codex-action@v1
- name: Run Code Review
run: |
codex exec "Review code changes in this PR" \
--output review.md
- name: Upload Review
uses: actions/upload-artifact@v4
with:
name: code-review
path: review.md
GitLab CI
GitLab CI configuration
span style="color: #0053A6;">
codex_review:
image: node:20
before_script:
- npm install -g @openai/codex
script:
- codex exec "Review changes in this merge request" --output review.md
artifacts:
paths:
- review.md
codex_review:
image: node:20
before_script:
- npm install -g @openai/codex
script:
- codex exec "Review changes in this merge request" --output review.md
artifacts:
paths:
- review.md
Jenkins
Jenkins Pipeline
pipeline {
agent any
stages {
stage('Code Review') {
steps {
sh 'codex exec "Review changed files" --output review.md'
}
}
}
post {
always {
archiveArtifacts 'review.md'
}
}
}
agent any
stages {
stage('Code Review') {
steps {
sh 'codex exec "Review changed files" --output review.md'
}
}
}
post {
always {
archiveArtifacts 'review.md'
}
}
}
Integrating Codex into CI/CD workflows enables automated code review and quality checks.
Best Practices
Output processing
- Usage
--outputSave results to file - Check the return code to determine whether execution succeeded
- Handle possible error cases
Performance Optimization
- Usage
gpt-5.4-miniReduce costs - Batch processing multiple tasks
- Set a reasonable timeout
Security
- Avoid using
--approval approve - Using read-only mode in CI/CD
- Protect API keys
When using non-interactive mode in production environments, pay special attention to security issues.
FAQ
Q: Does non-interactive mode support all features?
Most features are supported, but some features that require interactive confirmation may not be available.
Q: How are operations that require confirmation handled?
Usage--no-auto-approveparameter, or set the execution policy in the configuration file.
Q: Can the output format be customized?
Plain text output and Markdown output are currently supported.
Q: How to use it in Docker?
Install Codex into a Docker image, then invoke it in the CI/CD workflow.
other extensions