How to run Codex unattended with codex exec

Day 5, 25 minutes · beginner

Goal of this lessonTurn a repeat check into one codex exec command that runs read-only, writes a report file and can run from a script with no one watching.

Why it matters

Some jobs are the same every time: check the prices, summarize this week's changes, look for broken links. You should not have to open a session, type the prompt and wait for each one. Codex has a mode for that: codex exec runs one task from start to finish without the interactive screen and without stopping to ask, then exits.

Because it needs nobody at the keyboard, it fits in a script, a pipeline or a scheduled job. That also means the safety choices from lesson 3 matter more here, not less: you decide the sandbox up front, because there is no one to say yes or no halfway.

Today you turn the Puffinreach Kites price check into one codex exec command that runs read-only and writes its answer to a file. You test that it really catches a wrong price, and you save it as a small script. At the end you have an unattended check you can run any time with one line.

How codex exec behaves

From the Codex non-interactive mode docs:

  • One prompt in, one answer out. You pass the task as one argument. While it runs, Codex shows progress on the screen, but only the final message goes to the output, so you can save it or pass it on.
  • Read-only by default. Without other flags, codex exec runs in a read-only sandbox. For a run that may change files, add --sandbox workspace-write. Keep danger-full-access for a sealed environment, such as a throwaway CI runner.
  • A report file. -o (long name --output-last-message) writes the final message to a file and still prints it.
  • Machine-readable output. --json turns the output into one JSON event per line, for when another program reads the result.
  • Git is required. Codex refuses to run outside a git repository, to prevent damage. --skip-git-repo-check overrides that, for a folder you are sure about.
  • Your saved login is reused. The sign-in from lesson 1 also works for codex exec.

The steps

  1. Write the check as a prompt with an exact answer format. "If all prices match, answer only: All prices match." makes the report easy to read and easy for a script to test.
  2. Run it once and read the report file.
  3. Prove it works by breaking one price on purpose. A check that has never failed has not been tested.
  4. Undo the test change with git.
  5. Save the command as a script in the repository, so the exact same check runs every time.

Follow the guide for Codex below for the exact commands. Commit the script; you can leave the report file out of git.

Running it on a schedule

The Codex CLI has no scheduler of its own. The Codex docs put scheduled tasks in the ChatGPT desktop app and ChatGPT on the web, under Scheduled, and say the CLI is the place to prepare and test the prompt first. That is exactly what you did today. A desktop app task that works on a local folder needs the computer on and the app running.

For pipelines on a shared server, the docs advise an API key used only for the Codex step, or OpenAI's Codex GitHub Action for GitHub workflows, and never a key as a job-wide setting where other code can read it. Running and scheduling a coding agent unattended in general, with its risks, is covered in Claude Code in action: the ideas carry over to Codex.

Example: Benthe's Monday check

Benthe's first run wrote "All prices match." to price-report.md. Then she changed the Box Lantern to 45.00 in index.html and ran the line again. The report said: "Box Lantern: index.html 45.00, prices.json 49.50." She ran git restore index.html, saved the command as check-prices.ps1 on her Windows laptop and now runs it every Monday before she updates the shop.

Common mistakes

  • An open question as the prompt. "Check the site" gives a different essay every time. Ask for a fixed format.
  • Adding write access to a check. A check only needs to read. Leave the default read-only sandbox alone.
  • Trusting a check you never saw fail. Always break something once.
  • Running it from the wrong folder. codex exec works in the folder you start it from. Run the script from the repository, or pass the folder with --cd.

Today's actions

Ticks are not saved without an account

Do it in OpenAI Codex

OpenAI Codex

  1. In the terminal, go into puffinreach-kites. If you are still on the size-guide branch from lesson 4, switch back to main and get the merged version. (If you did lesson 4 in the terminal without GitHub, you are on main already: skip both lines.)
    git switch main
    git pull
  2. Run the check. codex exec runs read-only by default and writes its final message to the file after -o.
    codex exec "Compare every price in index.html with prices.json. List each kite whose price differs, with both values. If all prices match, answer only: All prices match." -o price-report.md
  3. Open price-report.md. It should say that all prices match. The progress you saw in the terminal is not in the file, only the final answer.
  4. Open index.html in your editor, change the Box Lantern price to 45.00 and save. Run the same codex exec line again and open price-report.md: it should name the Box Lantern with both prices.
  5. Undo your test change with git.
    git restore index.html
  6. Create a file called check-prices.sh (macOS or Linux) or check-prices.ps1 (Windows) in the repository with the same codex exec line as its only content, and save it.
  7. Run the script from the repository folder with sh check-prices.sh or .\check-prices.ps1. Check that price-report.md has a new date in your file explorer and still says all prices match.
  8. Commit only the script. The report file is output, so leave it out of git.
    git add check-prices.*
    git commit -m "Add price check script"

Note: codex exec refuses to run outside a git repository unless you add --skip-git-repo-check. For a run that may edit files, add --sandbox workspace-write. --json prints every event as a JSON line for other tools. If PowerShell blocks the script, the same line pasted into the terminal works too.

Paid planSame as lesson 1: the Codex CLI with ChatGPT Plus, Pro, Business, Edu or Enterprise, or with an OpenAI API key.

Checked on 4 December 2026 · Source: Codex docs: Non-interactive mode (opens in a new tab)

Create a free account to save your progress. Sign in or sign up