Running Agents 24/7
Patterns for keeping your agent running reliably in production. These are from real deployments — each one solves a problem we hit.
Watchdog with PID Files
A cron watchdog checks if your agent is alive and restarts it if it died. Do not use pgrep for detection — the pattern matching is unreliable. Use a PID file instead.
start.sh (run via cron every 5 minutes):
#!/bin/bash
PID_FILE="$AGENT_DIR/agent.pid"
if [ -f "$PID_FILE" ] && kill -0 "$(cat $PID_FILE)" 2>/dev/null; then
exit 0 # agent is alive
fi
# Clean up stale PID file and orphan processes
rm -f "$PID_FILE"
pkill -f "node.*agent" 2>/dev/null
sleep 1
cd "$AGENT_DIR" && nohup node agent.js >> "$LOG" 2>&1 &
echo "$!" > "$PID_FILE"Have the agent write its own PID file too:
import fs from 'fs';
// On startup
fs.writeFileSync('agent.pid', String(process.pid));
process.on('exit', () => {
try { fs.unlinkSync('agent.pid'); } catch {}
});Crontab entry:
*/5 * * * * /path/to/start.sh >> /path/to/logs/watchdog.log 2>&1Startup Cooldown
When an agent restarts, on-chain state may not be immediately visible (propagation delay, RPC cache). If the agent checks for positions, finds none, and opens a new one — each restart burns gas and may create duplicates.
Add a cooldown before any transaction-making logic:
const STARTUP_COOLDOWN_MS = 60000; // 60 seconds
const startupTime = Date.now();
// Before opening positions, placing trades, etc:
if (Date.now() - startupTime < STARTUP_COOLDOWN_MS) {
console.log('Startup cooldown — waiting for state to settle');
return;
}Stagger Cron Agents
If you run multiple agents on one machine, stagger their cron schedules by 3-5 minutes. Running multiple agents simultaneously can exhaust memory and cause the OS to kill processes.
# Good — staggered by 5 minutes
0 * * * * /path/to/agent-a/start.sh
5 * * * * /path/to/agent-b/start.sh
10 * * * * /path/to/agent-c/start.sh
# Bad — all at once
0 * * * * /path/to/agent-a/start.sh
0 * * * * /path/to/agent-b/start.sh
0 * * * * /path/to/agent-c/start.shDeployment Options
| Method | Effort | Cost | Best for |
|---|---|---|---|
| Local machine + tmux | Low | Free | Development |
| VPS + Docker + cron watchdog | Medium | $3-10/mo | Production agents |
| Railway / Fly.io / Render | Low | $5-25/mo | Managed hosting |
All methods use the same agent code. The difference is just how you keep it running.
Docker
FROM node:20-slim
RUN npm install -g @human.tech/waap-cli@latest
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
CMD ["node", "agent.js"]# docker-compose.yml
services:
agent:
build: .
env_file: .env
volumes:
- waap-session:/root/.waap-cli
restart: unless-stopped
volumes:
waap-session:Related
- Approvals & Notifications — Set up Telegram before deploying
- Cetus Yield Agent Recipe — Full recipe with deployment guide