Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Codex Non-Interactive Mode

Codex Non-Interactive Mode

Non-interactive mode lets you 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 a simple task

codex

exec

"List the 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 a pipe

echo

"Explain this code"

|

codex

exec

--stdin

Reading from a File

codex

exec

--stdin

<

task.txt

cat

<<

EOF

|

codex

exec

--stdin

Create a function to calculate factorial

Test this function

EOF

Non-interactive mode is ideal for automation tasks and script integration.


Command-Line Arguments

Common parameters

Parameters Description
exec "任务" Execute a specified task
--stdin Read tasks from standard input
--model, -m Specify model
--no-auto-approve Disable automatic confirmation
--output, -o Output to a file

Complete example

Complete parameter example

Specify model

codex

exec

-m

gpt-

5.4

-mini

"Review the code"

Output to a file

codex

exec

"Create a README"

-o

README.md

Disable automatic confirmation

codex

exec

--no-auto-approve

"Run the tests"

Compositional use

codex

exec

-m

gpt-

5.4

"Review the PR"

--output

review.md


Configure

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 in seconds

timeout = 300

execution policy

In non-interactive mode, the default policy is usually deny(reject operations that may have side effects):

Set the execution policy

Use the ask policy

codex

exec

--approval

ask

"Create a file"

Use the approve policy (use with caution)

codex

exec

--approval

approve

"Run the tests"

Use the deny policy (read-only)

codex

exec

--approval

deny

"Analyze the 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 the 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 the $file file"

--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

"Many files, use Codex for batch review"

    codex

exec

"Review all Python files"

--output

full_review.md

else

   

echo

"Few files, review one by one"

   

for

file

in

$

(

find

src

-name

"*.py"

)

;

do

        codex

exec

"Simply 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

GitLab CI

GitLab CI configuration

span style="color: #007F45;">

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'

       

}

   

}

}

Integrating Codex into CI/CD workflows enables automated code review and quality checks.


Best Practices

Output handling

Performance optimization

Safety

When using non-interactive mode in production, pay extra attention to security.


Common questions

Q: Does non-interactive mode support all features?

Most features are supported, but some that require interactive confirmation may not be available.

Q: How to handle operations that require confirmation?

Use --no-auto-approve parameter, or set the execution policy in the configuration file.

Q: Can the output format be customized?

Currently plain text output and Markdown output are supported.

Q: How do I use it in Docker?

Install Codex into a Docker image, then invoke it in CI/CD workflows.