Cloud VPS from $1.98/mo · pay with Bitcoin, Ethereum, Monero, USDT or Solana · paid in crypto
AI and machine learning

Run AI agents unattended on a VPS: services, timers, limits

Move AI agents off a laptop that sleeps: size the server, run them headless as systemd services or timers, and cap turns, time and spend on each run.

CloudSat teamNovember 18, 20266 min read
Abstract illustration of a crescent moon above a small server whose status lights keep blinking
On this pageWhy the laptop is the wrong home

The usual advice for keeping an AI agent alive overnight is to stop your laptop from sleeping. It works until the lid closes, the Wi-Fi changes or the battery runs out. For a job that keeps going while you are gone, a laptop is simply the wrong kind of machine. A small Linux server is the right one, and the agent needs far less hardware than most people assume, because the thinking happens somewhere else.

Why the laptop is the wrong home

  • Sleep wins. Power settings that keep a laptop awake still lose to a closed lid, an update reboot or a flat battery.
  • The network moves. A new IP address or a captive portal breaks webhooks and long connections halfway through a run.
  • It holds everything. Your laptop has your SSH keys, browser sessions and password manager. An agent with shell access there can reach all of it.
  • Nothing is logged. When a run fails at 3 a.m., a server has a journal you can read; a laptop usually has a closed terminal.

What the server actually needs

When an agent calls a hosted model, the model runs at the provider. Your machine runs the loop: it sends requests, waits, executes tools and stores results. Size it for the tools, not for the model.

Agent workload What runs on your server Starting point
Chat or messaging agent calling a hosted API The agent process and a small database 1 to 2 GB of RAM
Coding agent that runs tests and builds Git, toolchains, test suites 4 GB of RAM, 2 vCPU or more
Agent driving a headless browser One or more Chromium processes 4 to 8 GB of RAM
Agent using a local model A model runtime on a GPU A GPU sized for the model

Claude Code itself lists 4 GB of RAM as its minimum. A CloudSat Cloud VPS with 4 GB ($11.58/mo) covers the first three rows, and the last row belongs on a GPU server.

Run the agent without a terminal attached

Every serious coding agent has a non-interactive mode. These are the documented ones:

  • Claude Code: claude -p "task", with --output-format json, --max-turns and --max-budget-usd. It exits non-zero when a run fails.
  • Codex CLI: codex exec "task", read-only by default; add --sandbox workspace-write to allow edits and --json for a JSON Lines event stream.
  • OpenCode: opencode run "task".
  • Aider: aider --message "task" file.py.

For scheduled jobs, Claude Code's docs recommend --bare. It skips hooks, plugins, MCP servers and CLAUDE.md discovery, so every run starts the same way. Bare mode authenticates with ANTHROPIC_API_KEY or an apiKeyHelper, never with a subscription login. Without it, a subscription token created with claude setup-token and exported as CLAUDE_CODE_OAUTH_TOKEN works for scripts.

Option A: a nightly job with a systemd timer

A timer beats a crontab line for agents: output lands in the journal, a timeout stops runaway runs, and Persistent=true catches up on a run missed while the server was off. Start with the script the timer will call, /home/agent/bin/nightly-review.sh:

#!/usr/bin/env bash
set -euo pipefail
cd /home/agent/repo
git fetch --quiet origin
git checkout --quiet --detach origin/main
claude --bare -p "Review the commits merged into main in the last 24 hours. List bugs, risky changes and missing tests with file and line references." \
  --allowedTools "Read,Bash(git log *),Bash(git diff *),Bash(git show *)" \
  --max-turns 30 --max-budget-usd 3 --output-format json \
  | jq -r '.result' > "/home/agent/reviews/$(date +%F).md"

The agent can read files and inspect history, nothing more, and the report lands in a dated file. Install jq with sudo apt install -y jq and make the script executable. Then the service, /etc/systemd/system/nightly-review.service:

[Unit]
Description=Nightly AI code review
Wants=network-online.target
After=network-online.target

[Service]
Type=oneshot
User=agent
EnvironmentFile=/etc/agent/nightly-review.env
ExecStart=/home/agent/bin/nightly-review.sh
TimeoutStartSec=45min
KillSignal=SIGINT
NoNewPrivileges=yes
PrivateTmp=yes
ProtectSystem=full

And the timer, /etc/systemd/system/nightly-review.timer:

[Unit]
Description=Run the nightly AI code review

[Timer]
OnCalendar=*-*-* 02:30:00
RandomizedDelaySec=15min
Persistent=true

[Install]
WantedBy=timers.target

The environment file holds ANTHROPIC_API_KEY=..., owned by root with mode 0600; systemd reads it before switching to the agent user. For a one-shot service, TimeoutStartSec is the limit on the whole run. KillSignal=SIGINT matters for Claude Code: its docs say SIGTERM leaves the current turn unfinished, while SIGINT ends the turn first. Enable and check it:

sudo systemctl daemon-reload
sudo systemctl enable --now nightly-review.timer
systemctl list-timers nightly-review.timer
journalctl -u nightly-review.service --since today

If the timer fires while yesterday's run is still going, systemd does not start a second copy of the unit.

Option B: an always-on agent as a service

Messaging bots and agent gateways listen all day. Run them as a regular service that restarts on failure:

[Unit]
Description=Always-on agent
Wants=network-online.target
After=network-online.target

[Service]
User=agent
EnvironmentFile=/etc/agent/gateway.env
ExecStart=/home/agent/bin/start-agent.sh
Restart=on-failure
RestartSec=10
NoNewPrivileges=yes

[Install]
WantedBy=multi-user.target

Some agents install their own unit. OpenClaw's onboarding, openclaw onboard --install-daemon, sets up a background service for its gateway. If you prefer per-user units started with systemctl --user, run sudo loginctl enable-linger agent so they keep running after you log out.

Guardrails for runs nobody watches

  • Cap every dimension. Turns (--max-turns), estimated spend (--max-budget-usd), wall-clock time (TimeoutStartSec), and a spend limit on the API key at the provider. Claude Code's docs warn that spend can pass the cap, so leave headroom.
  • Allow tools by name. List the exact commands the job needs. Avoid permission-bypass flags on anything that runs unattended.
  • Treat repositories as input. Without --bare, a claude -p run executes hooks and connects MCP servers configured in the repository, with no trust prompt. Use bare mode on code you do not control.
  • Write to branches, never to main. A nightly job that edits code should push a branch and open a pull request for a human.
  • One key per job. If a job misbehaves, revoke its key without touching the others.
  • Get told about failures. Add OnFailure= to the unit to trigger a notification service, or end the script with a call to your alerting tool.

Long-running loops that you write yourself need more than this: checkpoints, retry budgets and circuit breakers. The agent loop guardrails guide covers them.

When cron is enough, and when to go further

A crontab line is fine for a short script you already trust:

30 2 * * * /home/agent/bin/nightly-review.sh >> /home/agent/logs/review.log 2>&1

It will not catch up after downtime, apply a timeout or keep structured logs, which is why timers suit agents better. When jobs depend on each other, need retries with backoff or fan out across many repositories, move to a workflow tool such as n8n, Apache Airflow or Dagu.

FAQ

Does the server need a GPU?

Only if the agent uses a local model. An agent that calls a hosted API needs CPU and memory for its tools, which a regular VPS provides.

Can scheduled runs use my subscription instead of an API key?

With Claude Code, claude setup-token creates a long-lived token for scripts that requires a paid Claude plan, but bare mode does not read it. codex exec reuses the CLI's saved sign-in by default. Check each vendor's terms for automated use before you rely on it.

How do I stop a runaway agent?

sudo systemctl stop nightly-review.service ends it now. The timeout and the turn and budget caps stop it on their own, and revoking its API key stops it everywhere.

What happens if the server reboots during a run?

The run is lost. Persistent=true only catches up on runs the timer missed while the server was off, not on runs that started and were cut short, so start it again with sudo systemctl start nightly-review.service. Design jobs so that running twice is harmless: work from a fresh checkout, write to dated files and push branches rather than editing shared state.

Run it on CloudSat. Cloud VPS on AMD EPYC and NVMe from $1.98/mo in 13 regions, delivered in under 5 minutes and paid in crypto. See plans or deploy a server.