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):

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

A short checklist

The handbook covers unattended jobs end to end: alerts, backups, and rules for what the agent may never do: Pocket Server.