Ubuntu 无头浏览器自动化(OpenClaw 全天候挂机)
概述
本 Skill 提供在 Ubuntu(VPS/云主机)上让 OpenClaw 全天候运行浏览器自动化的完整配置方案。
核心三件套:
1. systemd 服务化 — OpenClaw gateway 开机自启 + 崩溃自动拉起
2. 无头浏览器 — headless + noSandbox + 可执行路径,无桌面也能跑
3. 定时任务 — Cron(固定时间)或 HEARTBEAT(周期巡检),夜间自动干活
1. 禁止休眠/挂起
sudo systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target
验证:
systemctl status sleep.target suspend.target
2. OpenClaw 安装 & 服务化
安装
curl -fsSL https://openclaw.ai/install.sh | bash
守护服务(推荐)
openclaw onboard --install-daemon
验证:
systemctl --user status openclaw-gateway.service
手动创建 systemd 服务(备用)
如果 --install-daemon 在 root 环境有问题:
OPENCLAW_BIN="$(command -v openclaw)"
mkdir -p /run/user/0
chmod 700 /run/user/0
export XDG_RUNTIME_DIR=/run/user/0
mkdir -p /root/.config/systemd/user
cat > /root/.config/systemd/user/openclaw-gateway.service <<EOF
[Unit]
Description=OpenClaw Gateway (user)
After=network.target
[Service]
Type=simple
Environment=XDG_RUNTIME_DIR=/run/user/0
Environment=OPENCLAW_HOME=/root/.openclaw
WorkingDirectory=/root/.openclaw
ExecStart=$OPENCLAW_BIN gateway start
Restart=always
RestartSec=2
[Install]
WantedBy=default.target
EOF
systemctl --user daemon-reload
systemctl --user enable --now openclaw-gateway.service
常用管理:
# 启动/停止/重启
systemctl --user start openclaw-gateway.service
systemctl --user stop openclaw-gateway.service
# 看日志
journalctl --user -u openclaw-gateway.service -n 50
3. 无头浏览器配置
安装 Chromium(二选一)
方案 A — Playwright 自带(推荐):
npx playwright install chromium
# 路径类似:/home/用户名/.cache/ms-playwright/chromium-XXX/chrome-linux/chrome
方案 B — 系统包:
sudo apt update && sudo apt install -y chromium-browser
# 路径:/usr/bin/chromium-browser 或 /snap/bin/chromium
OpenClaw 浏览器配置
编辑 ~/.openclaw/openclaw.json,在顶层添加 browser 段:
{
"browser": {
"enabled": true,
"defaultProfile": "openclaw",
"headless": true,
"noSandbox": true,
"executablePath": "/home/你的用户名/.cache/ms-playwright/chromium-1208/chrome-linux64/chrome",
"profiles": {
"openclaw": {
"cdpPort": 18792,
"color": "#FF6B00"
}
}
}
}
> 关键参数说明:
> - headless: true — 无图形界面必须
> - noSandbox: true — 多数 Linux 云主机/容器需要关闭沙箱
> - executablePath — 换成实际安装路径
> - defaultProfile: "openclaw" — 托管模式,不需要扩展
或用 CLI 逐项设置:
openclaw config set browser.enabled true
openclaw config set browser.defaultProfile "openclaw"
openclaw config set browser.headless true
openclaw config set browser.noSandbox true
openclaw config set browser.executablePath "/home/用户名/.cache/ms-playwright/chromium-1208/chrome-linux64/chrome"
openclaw gateway restart
确保 browser 工具在白名单
{
"tools": {
"byProvider": {
"你的Provider名": {
"allow": ["read", "write", "edit", "web_search", "image", "browser"]
}
}
}
}
4. 验证浏览器
openclaw browser status
openclaw browser start
openclaw browser open https://www.baidu.com
返回 opened: https://www.baidu.com/ 即成功。
5. 夜间自动任务
5.1 Cron(固定时间触发)
openclaw cron add \
--name "night-auto-1" \
--cron "0 2 * * *" \
--tz "Asia/Shanghai" \
--session isolated \
--message "用浏览器打开 https://example.com,按流程抓取数据并保存到 ~/output/night.json,完成后发结果摘要" \
--announce \
--channel weixin:direct:你的ChatID
手动测试:
openclaw cron run night-auto-1
5.2 HEARTBEAT(周期巡检)
设置心跳间隔:
openclaw config set agents.defaults.heartbeat.every "30m"
编辑 ~/.openclaw/workspace/HEARTBEAT.md:
# 心跳任务
## 夜间浏览器自动化(23:00–05:00)
- 判断当前时间,不在 23:00–05:00 则跳过
- 在 23:00–05:00 时:
- 用浏览器打开目标网址,按流程抓取数据
- 追加到 ~/output/night-heartbeat.json
- 连续两次失败时立即告警
## 清理
- 删除 7 天前的 ~/output/night-heartbeat-*.log
6. 稳定性检查清单
| 项目 | 检查方式 |
|---|---|
| 服务状态 | systemctl --user status openclaw-gateway.service |
| 最近日志 | journalctl --user -u openclaw-gateway.service -n 100 --no-pager |
| 浏览器状态 | openclaw browser status |
| Cron 任务 | openclaw cron list |
| 休眠已禁 | systemctl status sleep.target |
常见问题
| 问题 | 解决方案 |
|---|---|
| 浏览器启动失败 | 确认 executablePath 正确 + noSandbox: true |
| cron 任务没跑 | 检查时区 --tz 是否与系统一致 |
| 内存不足被 OOM Kill | 检查 dmesg \| grep -i oom,考虑增加 swap |
| systemd --user 不工作 | 用备用的手动创建方案(第 2 节) |
| headless 截图空白 | 安装字体:sudo apt install fonts-noto-cjk |