Sub topic 1
What headless means and why scheduled jobs need it
A scheduled job runs when nobody is watching. This chapter defines what "headless" means for a coding agent and what changes when no one can answer a question. It also explains why those changes shape every later decision in this tutorial.
Prerequisites
This chapter assumes you can already do the following:
- Use a Linux or macOS shell and write short shell scripts.
- Use an interactive coding agent in a terminal, including approving or denying its tool requests.
- Work with Git branches, commits, and pull requests.
- Read and set environment variables, and check a process exit code with
$?.
You do not need to know cron, systemd, or CI scheduling yet. Later chapters cover them.
What "headless" means
A headless run is one where the agent starts, does its work, and exits without a person at the terminal. The word comes from "headless" hardware that runs with no monitor attached. For an agent, the missing part is the human who normally reads output and answers prompts.
In an interactive session, the agent and the person hold a conversation. The agent can:
- ask you a clarifying question,
- pause to request approval before running a command or editing a file,
- show you a half-finished result and wait for correction,
- rely on you to notice when it goes wrong and press Ctrl+C.
In a headless run, none of that is available. The agent receives its input once, produces output, and returns an exit status. Anything that would have required a person has to be decided before the run starts.
The exact way to start an agent non-interactively differs between products. Some take the prompt as a command-line argument, some read it from standard input, and some have a dedicated mode or flag. This tutorial uses the placeholder AGENT_CMD for that command. Check your agent's documentation for the real invocation.
Interactive and headless runs compared
| Aspect | Interactive session | Headless run |
|---|---|---|
| Who starts it | You, in a terminal | A scheduler or script |
| Terminal (TTY) attached | Yes | Usually no |
| Clarifying questions | Agent asks, you answer | Nobody can answer |
| Tool approvals | Prompted one by one | Must be configured in advance |
| Error handling | You see it and react | Exit status, logs, and alerts |
| Environment | Your shell profile and login | Whatever the scheduler provides |
| Review of results | Continuous | After the fact |
A TTY (teletype, a terminal device) is what lets a program show a prompt and wait for typing. Without one, a program that waits for input either fails immediately or waits forever, depending on how it is written. You can see which case you are in from a shell:
#!/usr/bin/env bash
# tty-check.sh: report whether this script has a terminal.
if [ -t 0 ]; then
echo "stdin is a terminal: interactive"
else
echo "stdin is not a terminal: headless-style"
fiRun it directly, then run it as ./tty-check.sh < /dev/null. The second run reports "headless-style". A scheduler gives your job the second kind of environment.
Why scheduled jobs need headless mode
A schedule triggers a job at a fixed time, such as 02:00 on weekdays. At that moment no one is guaranteed to be at a terminal. Three consequences follow.
- There is no one to answer. If the agent stops to ask "Should I continue?", the job stalls. If it is waiting on input that never arrives, it may hold a lock, use a runner, or consume budget until something kills it.
- There is no one to watch. A failure at 02:00 is discovered at 09:00, or never, unless the job reports it. Exit codes, logs, and alerts replace your attention.
- There is no one to undo a mistake in real time. An interactive agent that starts something wrong can be stopped. An unattended one runs to completion, so its permissions and outputs must be safe by design.
Typical jobs that fit this model are nightly dependency checks, a morning summary of open issues, test-failure triage, and documentation drift reports. They share a shape: a clear task, a bounded scope, and a result someone reviews later.
The anatomy of a headless job
A headless job is more than the agent command. It is a small system with a few parts that must each work without a person.
Each box maps to a later chapter:
- The scheduler decides when to run and what environment the job gets.
- The wrapper script is your code around the agent. It applies the lock and timeout and turns the result into an exit status the scheduler understands.
- The prompt and permissions replace the conversation. They say what to do and what is allowed.
- The checks decide whether the outcome is acceptable, and the alert brings a person back in only when needed.
A minimal first run
Before scheduling anything, run the agent by hand in a way that imitates a scheduler. This catches the most common failure early: a hidden prompt.
#!/usr/bin/env bash
# smoke-test.sh: run the agent with no terminal and a hard time limit.
# AGENT_CMD is a placeholder for your agent's non-interactive command.
# This script assumes the command reads the prompt from stdin.
set -u
: "${AGENT_CMD:?set AGENT_CMD to your agent's non-interactive command}"
echo "Reply with the single word: ready" > /tmp/smoke-prompt.txt
# AGENT_CMD is intentionally unquoted so a command with arguments splits into words.
timeout 2m $AGENT_CMD < /tmp/smoke-prompt.txt > /tmp/smoke.log 2>&1
status=$?
echo "exit status: $status"
echo "--- log ---"
cat /tmp/smoke.logReading the result:
- Status
0and a sensible log means the basic headless path works. - Status
124meanstimeoutkilled the process after two minutes. The agent was probably waiting for input or approval that never came. - Any other non-zero status is the agent's own failure. The log usually says why, such as a missing credential or an unknown option.
Use a trivial prompt first. If a trivial prompt hangs, a real prompt will too, and you have learned that without risking changes to a repository.
Common errors
The job hangs. The agent asked a question or requested a tool approval. The fix is not to feed it answers. Remove the cause: make the prompt self-contained and pre-approve only the tools the task needs. Always wrap the run in timeout so a hang becomes a visible failure instead of an endless wait.
The job works in your shell and fails under the scheduler. Your interactive shell loads a profile, a longer PATH, and exported variables. A scheduler provides little of that. Chapter 3 covers this in detail. For now, remember that a headless job must declare everything it depends on.
The job "succeeds" but did nothing useful. An agent can exit with status 0 after declining to act, because a needed permission was denied or the task was ambiguous. Exit status alone reports that the process ended, not that the work was good. You need output checks as well.
The job succeeds, but nobody can tell what happened. Without a log, you cannot reconstruct a run after the fact. Write each run's output to a timestamped file from the first day.
Granting everything to make the prompts stop. Disabling all approvals is the quickest way to end a hang, and also the most dangerous. An unattended agent with broad permissions can change or delete far more than a nightly task needs. Approve the narrowest set of tools that completes the task.
Design rules to carry forward
Treat these as the working model for the rest of the tutorial:
- Every decision a person would make interactively must be made in advance, in the prompt or in configuration.
- Every failure must produce a signal: a non-zero exit status, a log entry, or an alert.
- Every run must be bounded in time and in permissions.
- Results should land where a person reviews them, such as a branch or pull request, rather than directly in a protected branch.
Exercise
Your nightly agent job runs for hours at 02:00 and never finishes. Running the same command in your terminal works. Name two likely causes related to headless operation, and one measure that turns the hang into a visible failure.
Show answer
Likely causes: the agent is waiting for a tool approval or a clarifying answer that nobody can give because no terminal is attached, or it depends on environment (such as PATH or credentials) that exists in your shell but not under the scheduler. Wrapping the run in timeout makes a hang end with exit status 124, which the wrapper can log and report. Fixing the cause means a self-contained prompt, pre-approved tools, and an explicit environment.
Exercise
A headless run exits with status 0, but the repository has no changes and the log says a command was "not permitted". Why is exit status alone not enough to judge this run, and what should you add?
Show answer
Status 0 only shows that the agent process ended normally. The agent can finish after being denied a tool and report that it could not do the task. Add checks on the outcome, such as validating a structured result file or confirming the expected branch or commit exists, and alert when the check fails.