1
Install on your server
Run npm install -g @anthropic-ai/claude-code on your VPS or CI runner. Works on any Linux or macOS machine with Node.js 18+.
2
Set up authentication
Store your Anthropic API key in an environment variable (ANTHROPIC_API_KEY). For subscription users, generate a long-lived token with claude setup-token.
3
Write your automation script
Use claude --bare -p with your prompt, --allowedTools for permissions, --max-budget-usd for cost caps, and --output-format json for structured output.
4
Schedule with crontab
Add your script to crontab for recurring runs. The job executes independently of your laptop, running on the VPS with your API key.
| Permission Mode | What it auto-approves | Best for | |
|---|---|---|---|
| default | default | Reads only | Sensitive work, maximum safety |
| acceptEdits | acceptEdits | Reads + file edits + basic filesystem | Code modifications with some guardrails |
| auto | auto | Everything (with safety classifier) | Background automation with safety checks |
| dontAsk | dontAsk | Only explicit allow rules | Locked-down CI, maximum control |
| bypassPermissions | bypassPermissions | Everything (no checks) | Isolated containers only, never on production |
Security considerations for unattended execution
Never use bypassPermissions on a machine connected to the internet or with access to production systems. It disables ALL safety checks. For most automation,
auto mode (with its background safety classifier) or dontAsk mode (with explicit allow rules) provides the right balance of autonomy and safety. Always use --max-budget-usd to cap costs and --allowedTools to restrict tool access.Getting started with headless automation
Start simple: run
claude -p "What does the auth module do?" in your terminal to see print mode in action. Then try piping data: cat error.log | claude -p "explain the root cause". Once comfortable, add --bare for deterministic behavior, --output-format json for structured output, and --allowedTools "Read,Bash(git *)" for controlled tool access. Build up from there to full cron-based automation.