DEV Community

Ciotalk
Ciotalk

Posted on

Before You Put an AI Agent in Cron: 6 Rules for Running Claude Code Unattended

AI coding agents are moving out of the chat window. Claude Code, for example, has a non-interactive mode: you run claude -p "your prompt", it does the job, prints the result and exits. That makes it easy to drop into a cron job, a Git hook or a CI pipeline.

It also removes the one safety feature most people relied on without thinking about it: a human reading each action and clicking “approve”. Once the agent runs at 3 a.m. on a build server, the guardrails have to be in the config, not in your attention span.

Here are the rules worth settling before the first scheduled run.

  1. Pick jobs where reading is the hard part

The best fits are boring. A person reads a lot of text and writes back a little: a summary of last night’s SSH logs, a review of a Terraform plan, a weekly changelog from merged commits, a digest of backup reports nobody opens.

Poor fits are open-ended tasks where the next step depends on what the agent finds, like debugging an outage or refactoring an old service. Those still need someone in the loop. If you can’t describe the expected output in one sentence, the job probably isn’t ready to be automated.

  1. Give it data, not access

The safest pattern is to pipe the input in instead of letting the agent go and fetch it:

bash
git diff main | claude -p "Review this diff for security issues. Be brief."

Here Claude never needs permission to run git, open files or touch the shell. It reads what you hand it and answers. Compare that with telling it to “go check the repo” and allowing Bash so it can. The output might be similar, but the second setup can do far more damage if something goes wrong.

When the agent truly needs tools, list them one by one. Claude Code’s --allowedTools flag accepts narrow rules like Bash(git diff *), which allows that command and nothing else.

  1. Assume the input is hostile

This is the rule teams skip, and it’s the one that matters most. Logs, tickets, emails, pull requests and web pages are written by people you don’t control. Any of them can contain text that reads like an instruction to the model. A login attempt with a crafted username is enough to try it.

You can’t fully filter that out. What you can do is make a successful injection boring. If the agent has no tools, the worst case is a strange summary in your inbox. If it has shell access, the worst case is much worse. Rule 2 is your real defence against rule 3.

  1. Run it as nobody important

No root, no admin account, no personal credentials. Create a dedicated user that can read exactly what the job needs. For CI, run inside the container the pipeline already uses. Claude Code has a mode that skips permission checks entirely; on a production machine that hands the agent everything the account can do, so leave it off.

Also watch what the repository itself brings. By default, a non-interactive run loads the project’s hooks and MCP server config without asking. On a CI runner building someone else’s pull request, that’s an open door. The --bare flag starts the agent clean and only loads what you pass on the command line, which is what you want on shared infrastructure.

  1. Make the output machine-readable

Model output isn’t deterministic. Ask the same question twice and you’ll get two slightly different paragraphs. That’s fine for a human reading an email and terrible for a script that greps the result.

Ask for JSON instead. With --output-format json you get the answer in a result field plus metadata like the session ID and an estimated cost. If downstream code expects fixed fields, --json-schema makes the answer follow a schema you define. Then your pipeline checks a value, not a sentence.

  1. Put a ceiling on cost

Every run is billed. One daily summary costs very little; the same pattern copied to forty servers and every pull request adds up fast. Check the cost field for a week before scaling a job, use a smaller model for routine summaries, and set a hard budget limit per run so a confused agent can’t loop forever.

The short version

Narrow job, input piped in, no tools or very few, low-privilege account, structured output, budget cap. None of it is exotic. It’s the same least-privilege thinking you’d apply to any service account, just pointed at something that reads instructions in plain English.

If you want the full rundown of flags, permission modes and CI setup, I wrote a longer guide on https://ciotalkradio.com/claude-code-headless-mode/

Top comments (0)