Run Claude Code unattended with cron or systemd
The non-interactive call, the flags that bound it, and why it fails the first time from cron.
The point of running Claude Code on a server is often that it works while you do not: a nightly review, a morning summary, a job that reacts to a file. Claude Code has a non-interactive mode for this. This guide shows the one-line form, the flags that keep an unattended run bounded, and what usually goes wrong the first time it runs from cron or a systemd timer.
The non-interactive call
claude -p "your prompt" (long form --print) runs one prompt, prints the answer and exits. There is no prompt to approve at the keyboard, so every permission question has to be answered in advance by your settings and flags. With --output-format json you get structured output with the result, a session ID and metadata, which a script can parse:
claude -p "Summarise the failed units in the log below" --output-format json < /tmp/failed.txt
Keep the session ID if a later run should continue the same conversation: extract it with jq -r '.session_id' and pass it to --resume.
Bound the run before you schedule it
An unattended run has no human to stop it, so put the limits on the command line. These flags exist in the current CLI (check claude --help on your version, flags change):
--max-budget-usd 2: a dollar cap for the run (works with--print). A loop or an over-eager task ends at the cap instead of at the end of the month.--allowedToolsand--disallowedTools: name exactly the tools the job needs. A summary job needsRead, notBash.--permission-mode: pick the narrowest mode that lets the job finish (plan,acceptEditsanddontAskare among the choices). Keep the mode that skips all checks for isolated containers only.--model: choose a cheaper model for routine summaries.
claude -p "Review yesterday's commits for leaked secrets" --output-format json --max-budget-usd 2 --allowedTools "Read" "Grep" "Bash(git log:*)" --permission-mode dontAsk
Test the same command by hand, as the user that will run it, before you schedule it.
A systemd timer instead of cron
A timer gives you logs in the journal and a failure state you can alert on. Run it as the dedicated non-root user, not as root.
# /etc/systemd/system/nightly-review.service
[Unit]
Description=Nightly Claude Code review
OnFailure=notify-failure@%n.service
[Service]
Type=oneshot
User=claude-work
WorkingDirectory=/srv/claude-work/project
ExecStart=/srv/claude-work/bin/nightly-review.sh
RuntimeMaxSec=1200
# /etc/systemd/system/nightly-review.timer
[Timer]
OnCalendar=*-*-* 03:30:00
Persistent=true
[Install]
WantedBy=timers.target
Put the claude -p call in nightly-review.sh and make the script exit non-zero when the command fails or the output is empty. RuntimeMaxSec is the second cap next to the dollar cap. The OnFailure= unit is yours to write: anything that sends you a message.
What breaks the first time
- Not logged in for that user. The scheduled job runs as a different user or without your shell environment. Log in once as that user, or provide an API key through the unit's
EnvironmentFile=(mode 600, never in the script). - Wrong working directory. Claude Code reads
CLAUDE.mdand project settings from the directory it starts in. SetWorkingDirectory=(orcdin the script). - Command not found. cron and systemd start with a minimal
PATH. Use the absolute path ofclaude(seecommand -v claude), or setPATHin the unit. - A hook you rely on is skipped. The
--bareflag skips hooks, plugins, auto-memory andCLAUDE.mddiscovery. Do not use it on a job whose safety depends on your deny hook. - Silent failure. A job that fails at 03:30 and tells nobody is the worst outcome. Make failure loud: non-zero exit plus an alert that reaches you.
- Untrusted input. If the prompt includes text from outside (mail, tickets, web pages), treat it as data: give the run read-only tools and no secrets. A narrow tool list is what limits the damage of a hostile sentence in that text.
A short checklist
- Dedicated non-root user; credentials in a root-owned mode-600 file.
- Dollar cap, timeout and an explicit tool list on every scheduled call.
- Absolute paths, a set working directory, run once by hand first.
- Non-zero exit on failure and an alert that reaches a human.
- Deny hook and permission rules in place, and no
--barewhere they matter.