Sub topic 1
What Headless Means and Why Scheduled Jobs Need It
Prerequisites
This chapter assumes you have:
- Used a shell on Linux or macOS and written short shell scripts.
- Used an interactive coding agent in a terminal.
- Basic Git knowledge: branches, commits, and pull requests.
- Some familiarity with environment variables and process exit codes.
What headless means
In an interactive session, you type a request. The agent proposes actions and asks permission before it edits files or runs commands. You read the output and decide what to do next.
A headless run (also called non-interactive) removes you from that loop. A program starts the agent, hands it a prompt from a file, standard input, or an argument, and waits for the process to end. The agent runs to completion, writes its output somewhere, and exits with a status code. Nobody is present to answer a question or approve an action.
What changes when nobody is watching
| Concern | Interactive session | Headless run |
|---|---|---|
| Questions | The agent can ask you | Nobody answers, so the prompt must be complete |
| Permissions | You approve actions as they come up | Allowed tools must be set before the run starts |
| Output | You read it on screen | It goes to a log or a file |
| Failure signal | You see an error and react | The exit code and logs are the only signals |
| Environment | Your shell, your login | Whatever the scheduler provides |
Each row maps to a later chapter. For now, the key point is that every decision you would make live must be made in advance.
Why scheduled jobs need it
A scheduler such as cron starts a process at a set time, for example 02:00. There is no terminal and no person. If the agent stops to ask for approval, it waits for input that never comes. The job hangs until something kills it, and you may not notice until morning.
Headless mode solves this, but only if you do the setup work. The prompt must be self-contained. Permissions must be pre-approved and kept narrow. The job must report success or failure in a way a script can read.
A minimal headless run
The script below is the smallest useful shape. AGENT_CMD is a placeholder for your agent's non-interactive command. Check your agent's documentation for the exact command and flags, because they differ between tools. This script assumes the agent reads the prompt from standard input.
#!/usr/bin/env bash
set -u
: "${AGENT_CMD:?set AGENT_CMD to your agent's non-interactive command}"
cd /srv/repos/my-project || exit 1
# AGENT_CMD is unquoted on purpose so a command with arguments splits into words.
$AGENT_CMD < prompts/nightly.md > run.log 2>&1
status=$?
echo "agent exited with status $status"
exit "$status"The script does four things. It fails early if the command is not configured. It moves to a known directory. It sends the prompt in from a file and the output to a log. It passes the agent's exit status on to whatever launched the script. A scheduler can then treat a non-zero status as a failure.
Common errors at this stage
- Using a prompt that says "ask me which files to change". Nobody can answer.
- Relying on settings stored in your interactive session instead of in files the job can read.
- Discarding the exit status, so a failed run looks like a successful one.
Exercise
Name two things that an interactive session handles through conversation but a headless run must settle before it starts.
Show answer
Permissions (which tools and actions are allowed) and missing information (the prompt must be self-contained). Either answer pair is correct. Failure reporting through exit codes and logs is a third.