OpenClawAI / start_openclaw.sh
R1000's picture
Update start_openclaw.sh
935ed9d verified
#!/bin/bash
# ปิด Script ทันทีหากเกิด Error และห้ามใช้ตัวแปรที่ไม่ได้ประกาศ
set -euo pipefail
# 1. สร้างไดเรกทอรีที่จำเป็น (ใช้บรรทัดเดียวรวมกัน)
mkdir -p /root/.openclaw/{agents/main/sessions,credentials,sessions}
# 2. เรียกคืนข้อมูล (Restore) ก่อนเริ่มงาน
python3 /app/sync_manager.py restore
# 3. จัดการ API Base URL ให้สะอาด (ลบ /v1 หรือ /chat/completions ที่เกินมา)
# ใช้ Parameter Expansion ของ bash แทน sed เพื่อความเร็ว
CLEAN_BASE="${OPENAI_API_BASE%/chat/completions}"
CLEAN_BASE="${CLEAN_BASE%/}"
CLEAN_BASE="${CLEAN_BASE%/v1}"
# 4. สร้างไฟล์ openclaw.json (ตัดคอมมาเกิน และจัดรูปแบบให้เป๊ะ)
if [ ! -f "/root/.openclaw/openclaw.json" ]; then
echo "🆕 Creating new openclaw.json from environment variables..."
cat > /root/.openclaw/openclaw.json <<EOF
{
"logging": {
"level": "debug",
"consoleLevel": "debug",
"consoleStyle": "pretty",
"redactSensitive": "tools"
},
"session": { "dmScope": "per-channel-peer" },
"models": {
"providers": {
"nvidia": {
"baseUrl": "${CLEAN_BASE}/v1",
"apiKey": "$OPENAI_API_KEY",
"api": "openai-completions",
"models": [
{ "id": "$MODEL", "name": "$MODEL", "contextWindow": 128000 }
]
}
}
},
"agents": { "defaults": { "model": { "primary": "nvidia/$MODEL" } } },
"commands": { "restart": true },
"gateway": {
"mode": "local",
"bind": "lan",
"port": $PORT,
"trustedProxies": ["0.0.0.0/0"],
"auth": { "mode": "token", "token": "$OPENCLAW_GATEWAY_PASSWORD" },
"controlUi": {
"allowedOrigins": ["https://r1000-openclawai.hf.space"],
"enabled": true,
"allowInsecureAuth": true,
"dangerouslyDisableDeviceAuth": true,
"dangerouslyAllowHostHeaderOriginFallback": true
}
},
"tools": {
"agentToAgent": {
"enabled": true,
"allow": ["assistant", "coder", "designer"]
},
"allow": ["exec", "read", "write", "edit", "process", "bash", "sessions_spawn", "sessions_send", "sessions_list", "message", "gateway", "cron"],
"elevated": {
"enabled": true,
"allowFrom": { "line": ["*"], "discord": ["*"] }
}
}
}
EOF
fi
# 7. ตั้งค่าสิทธิ์ไฟล์
chmod -R 700 /root/.openclaw
# 5. เริ่มระบบ Backup อัตโนมัติ (ใส่ & เพื่อรัน background)
python3 sync_manager.py backup &
# 6. ตรวจสอบระบบและรัน Gateway
openclaw doctor --fix
echo "🚀 Starting OpenClaw Gateway on port $PORT..."
exec openclaw gateway run --port "$PORT"