Disclosure: this article was written with AI assistance. It was drafted by a Claude Code agent for the Keelwork account, and the account owner approved publishing it. The commands, flags and numbers below come from real runs and tests; nothing is invented.
Claude Code does not need you at the keyboard. claude -p (print mode) takes a prompt, runs, prints the result and exits, so you can start it from cron, launchd, a systemd timer or CI. A morning summary of yesterday's commits, a digest of new TODOs, a first pass at release notes: small jobs like these are a good fit.
The catch is that an unattended run has nobody to say "no". In an interactive session you see each risky command before it runs. At 07:30 on a Tuesday, the only thing between the agent and your repository is the command line you wrote weeks ago. Here is what I put on that command line, and what I learned testing it.
The command line
This is the exact command a scheduled job of mine runs (Claude Code 2.1.x; paths shortened):
claude --print \
--output-format json \
--permission-mode dontAsk \
--max-budget-usd 0.5 \
--settings .headless/profiles/unattended.settings.json \
--allowedTools "Read(./**)" "Grep(./**)" "Glob(./**)" \
"Bash(python3 .headless/bin/git-ro:*)" \
"Edit(./reports/**)" \
< prompts/daily-report.md
Line by line:
-
--printruns once and exits instead of opening the interactive UI. -
--output-format jsongives one JSON object at the end. It includestotal_cost_usd, which is what you want in a log file. -
--permission-mode dontAskis the important one. Anything not on the allow-list is refused instead of waiting for an answer that will never come. Do not rely on whatever default your global settings happen to have: a job that silently inheritsbypassPermissionsfrom someone's laptop config is how surprises happen. -
--max-budget-usd 0.5caps the spend for this run. -
--settingsloads an extra settings file withdenyrules (more below). -
--allowedToolslists exactly what this job may use. Note the(./**)onRead,GrepandGlob. Without a scope, those rules can read files outside the project. - The prompt comes from a file on standard input, so it can be reviewed and versioned like code.
The cost cap is a cap, not a guarantee
Claude Code checks the budget as the run goes, not before each token, so a run can end slightly above it. In my test a $0.10 cap stopped a run at $0.1065. Plan for a small overshoot.
For scale: small daily-report runs on the haiku model cost me between $0.07 and $0.22 each. Your numbers will depend on the model, the size of the repository and the prompt, so log total_cost_usd from the JSON output for a week before you trust any estimate, including mine.
If you add retries, the worst case per scheduled run becomes budget × (1 + retries). I do not retry runs that were stopped by the cost cap; a retry would only spend the same budget again on the same task.
A deny list as a second layer
The allow-list is the main control. The settings file adds deny rules as a backstop, in case someone later widens the allow-list without thinking:
{
"permissions": {
"deny": [
"Bash(git push:*)",
"Bash(rm -rf:*)",
"Bash(sudo:*)",
"Bash(curl:*)",
"Bash(wget:*)",
"Read(./.env)",
"Read(./.env.*)",
"Read(**/*.pem)",
"Read(**/id_rsa)",
"WebFetch",
"WebSearch"
]
}
}
This is the whole file I use. Deny rules match command text, so they are a seatbelt, not a sandbox.
The shell commands that quietly widen the allow-list
This was the part that surprised me. I first gave the report job a few "harmless, read-only" shell commands: git log, git diff, head, sort. Then I went through their options:
-
git diff --output=FILEwrites a file. So doessort -o FILE. -
git diff --no-index /some/path /other/pathcompares two arbitrary files, including files outside the repository. -
head ../filereads outside the project, andBash(head:*)allows it.
Every allowed shell command is a door, and its options decide how wide. "Read-only" is a property of a specific invocation, not of a program.
What I ended up doing: the report jobs get no general shell commands at all. They reach git through one small wrapper script that accepts only a fixed list of read-only subcommands (log, show, diff, status, blame and a few more), each with its own short allow-list of options, accepts options with values only in the --name=value form (the one exception is -n <number>), and refuses any path that is absolute, starts with ~, contains .., or is a symlink pointing out of the repository. The job's allow-list then contains just Bash(python3 .headless/bin/git-ro:*). It is the piece I would review first.
The only place the job may write is Edit(./reports/**).
Don't run the same job twice at once
If a run takes longer than its interval, or you run it by hand while the timer fires, two agents edit the same report. A lock fixes that. Here is a minimal version for any POSIX shell (mkdir is atomic, so it works on macOS, which has no flock by default):
#!/bin/sh
# run-job.sh JOB - run one Claude Code job, never two copies at once
set -eu
job="$1"
claude_bin="${CLAUDE_BIN:-$HOME/.local/bin/claude}" # cron has a minimal PATH
lock=".run-job-$job.lock" # inside the project folder
if ! mkdir "$lock" 2>/dev/null; then
echo "$job is already running (or a stale $lock was left behind)" >&2
exit 1
fi
trap 'rmdir "$lock" 2>/dev/null || true' EXIT
trap 'exit 1' INT TERM
mkdir -p logs
"$claude_bin" --print --output-format json \
--permission-mode dontAsk --max-budget-usd 0.5 \
--settings unattended.settings.json \
--allowedTools "Read(./**)" "Grep(./**)" "Glob(./**)" "Edit(./reports/**)" \
< "prompts/$job.md" > "logs/$job-$(date +%Y%m%d-%H%M%S).json"
unattended.settings.json is the deny-list file from above. Set CLAUDE_BIN if command -v claude prints a different path. If the process is killed with kill -9 or the machine loses power mid-run, the lock folder stays behind and later runs refuse to start; delete .run-job-<job>.lock by hand after checking that nothing is running.
And a cron line that runs it at 07:30 on weekdays:
30 7 * * 1-5 cd /path/to/project && sh ./run-job.sh daily-report
On macOS, launchd is the better scheduler because it runs a missed job after the machine wakes up; on Linux, a systemd timer with Persistent=true does the same. cron simply skips runs while the computer is asleep.
Two things that bite in scheduled environments: cron starts with a minimal PATH, which is why the script uses the full path to claude, and the job runs as your user with your login, so it spends from your own plan or API key.
Limits you should know
-
run-job.shdoes not sandbox anything. The agent runs with your user's permissions; the allow-list and deny rules are enforced by Claude Code, so keep Claude Code updated. - A prompt can still ask the agent to do something unwise with the tools you allowed. Allow only what a job needs, and read your prompts like code.
- If you run jobs in CI, anything the job writes into an uploaded artifact is uploaded with it.
- For extra safety, run jobs on a separate clone of the repository or inside a container.
Summary
| Setting | Why |
|---|---|
--permission-mode dontAsk |
Unknown tools are refused instead of waiting forever |
--max-budget-usd |
Caps spend per run (allow a small overshoot) |
Scoped Read(./**) etc. |
Unscoped read rules reach outside the project |
| No general shell commands | Options like --output, -o, --no-index widen "read-only" commands |
| A lock | Two copies of a job never edit the same files |
| JSON output in a log |
total_cost_usd per run, so you know the real cost |
Independent project; not affiliated with or endorsed by Anthropic. "Claude" and "Claude Code" are trademarks of Anthropic.
Top comments (0)