























第一次接触 OpenClaw CLI 时,我被它的命令数量震惊了——50+ 个子命令,从 setup 到 browser,从 agent 到 nodes,每个都有自己的子命令树。但真正用起来才发现,这不仅仅是命令多,而是设计精良的瑞士军刀,每个命令都在解决实际问题。
我用 OpenClaw CLI 已经半年了,从最初连 setup 都需要查文档,到现在能熟练用 agent spawn 和 browser snapshot 自动化工作流。这篇文章整理了我最常用的命令和实战技巧,帮你少走弯路。
OpenClaw 是一个自托管的 AI Gateway,能让你从任何聊天应用(Discord、Telegram、WhatsApp 等)访问 AI 代理。但真正的威力在于它的 CLI 工具——你不需要打开浏览器,就能控制整个 Gateway:启动停止、管理会话、调用 Agent、甚至控制浏览器。
CLI 的优势在于:
--json 输出格式,可以轻松解析结果OpenClaw 需要 Node 22+ 环境,安装非常简单:
npm install -g @openclaw/cli
安装完成后,验证版本:
openclaw --version
运行向导式配置:
openclaw setup
这个命令会引导你完成:
- 选择 AI Provider(OpenAI、Anthropic、本地模型等)
- 配置 API Keys
- 设置通道(Discord、Telegram 等)
- 选择部署模式(开发模式还是生产模式)
如果你想完全自动化安装(CI/CD 场景),可以使用配置文件:
openclaw configure --file config.yaml
配置完成后,启动 Gateway:
openclaw gateway start
查看 Gateway 状态:
openclaw gateway status
openclaw status — 一键查看系统状态这是我用得最多的命令,快速了解 Gateway 运行状况:
openclaw status
输出包含:
- Gateway 运行状态(运行中/已停止)
- 当前活跃会话数
- 内存和 CPU 使用情况
- 最后错误信息
如果你想要机器可读的输出:
openclaw status --json
openclaw agent spawn — 创建新的 Agent 会话这是 OpenClaw 的核心功能,启动一个隔离的 AI 代理会话:
openclaw agent spawn "帮我分析这个 GitHub 仓库的架构"
常用参数:
--runtime subagent — 使用子代理运行时(默认)--runtime acp — 使用 ACP(AI Coding Platform)运行时--model claude-4-sonnet — 指定使用的模型--session-label my-analysis — 给会话打标签openclaw sessions list — 管理所有会话查看当前所有活跃会话:
openclaw sessions list
如果你想查看最近 5 分钟内的活跃会话:
openclaw sessions list --active-minutes 5
查看某个会话的历史消息:
openclaw sessions history --session-key <session-key>
openclaw message send — 直接发送消息如果你想直接向某个通道发送消息,而不通过聊天应用:
openclaw message send --channel discord --target "general" "大家好!"
这个命令在自动化通知时非常有用。
openclaw browser start — 启动浏览器自动化OpenClaw 内置了浏览器控制功能,可以自动化 Web 操作:
openclaw browser start
打开指定 URL:
openclaw browser open --url "https://example.com"
截图:
openclaw browser screenshot
openclaw models list — 查看可用模型查看当前配置的所有 AI 模型:
openclaw models list
设置默认模型:
openclaw models set claude-4-sonnet
查看模型状态和配额:
openclaw models status
openclaw gateway logs — 查看日志调试时必不可少:
openclaw gateway logs --tail 100
实时跟踪日志:
openclaw gateway logs --follow
openclaw skills search — 查找和安装 SkillsOpenClaw 的 Skills 是预制的 AI 能力包,可以极大扩展功能:
搜索 Skill:
openclaw skills search "github"
安装 Skill:
openclaw skills install gh-issues
查看已安装的 Skills:
openclaw skills list
openclaw nodes list — 管理移动节点如果你配置了 iOS 或 Android 移动节点,这个命令很重要:
openclaw nodes list
批准待配对的节点:
openclaw nodes approve <node-id>
查看节点详细信息:
openclaw nodes describe <node-id>
openclaw security audit — 安全审计定期运行安全审计,检查配置漏洞:
openclaw security audit
深度扫描(会探测 Gateway):
openclaw security audit --deep
自动修复常见安全问题:
openclaw security audit --fix
OpenClaw 的强大之处在于同时支持多个聊天通道。查看所有通道状态:
openclaw channels status
添加新通道:
openclaw channels add telegram --token <your-token>
测试通道连接:
openclaw channels probe telegram
创建一个自动化工作流,每天早上生成 AI 日报并发送到指定频道:
#!/bin/bash
# 每天早上 9 点运行
# 1. 生成日报
REPORT=$(openclaw agent spawn --runtime acp --model claude-4-sonnet \
"生成今日 AI 技术日报,包含 10 条要闻" --json)
# 2. 发送到 Discord
openclaw message send --channel discord \
--target "ai-news" \
"$REPORT"
echo "日报已发送"
保存为 daily-report.sh,然后添加到 crontab:
0 9 * * * /path/to/daily-report.sh
openclaw --profile production gateway start
openclaw --profile dev gateway start
这样你可以在开发环境和生产环境使用不同的配置。
在脚本中,始终使用 --json 参数,方便解析结果:
STATUS=$(openclaw gateway status --json)
RUNNING=$(echo $STATUS | jq '.running')
使用 xargs 或 for 循环批量处理:
# 批量删除旧会话
openclaw sessions list --json | \
jq -r '.sessions[] | select(.age_days > 7) | .key' | \
xargs -I {} openclaw sessions kill {}
Gateway 启动失败:
# 1. 检查端口占用
openclaw doctor
# 2. 查看详细日志
openclaw gateway logs --follow
# 3. 尝试重置
openclaw gateway restart
Agent 无响应:
# 查看会话状态
openclaw sessions list --active-minutes 1
# 如果卡死,强制结束
openclaw sessions kill <session-key>
浏览器自动化失败:
# 检查浏览器状态
openclaw browser status
# 重置浏览器
openclaw browser reset-profile
# 每周一次
0 0 * * 0 openclaw security audit --deep
# 添加 secret
openclaw secrets add api-key <value>
# 在配置中引用
openclaw config set openai.api-key secret:api-key
# 只允许特定用户
openclaw approvals set --mode allowlist
openclaw approvals allowlist add user@example.com
# 创建备份
openclaw backup create
# 验证备份
openclaw backup verify <backup-file>
我最近用 OpenClaw CLI 构建了一个自动化代码审查流程,每天晚上自动检查 GitHub 仓库的 PR:
#!/bin/bash
# 配置
REPO="owner/repo"
DISCORD_CHANNEL="code-review"
# 获取今天的 PRs
PRS=$(gh pr list --repo $REPO --state open \
--json title,url,author | jq -c '.')
# 用 AI 分析每个 PR
echo "$PRS" | jq -r '.[] | @json' | while read -r pr; do
TITLE=$(echo $pr | jq -r '.title')
URL=$(echo $pr | jq -r '.url')
REVIEW=$(openclaw agent spawn --runtime acp \
"审查这个 PR 的代码质量:$URL" --json)
# 发送到 Discord
openclaw message send --channel discord \
--target "$DISCORD_CHANNEL" \
"**PR Review: $TITLE**\n$URL\n\n$REVIEW"
done
这个脚本每天凌晨 2 点运行,第二天早上我就能在 Discord 看到所有 PR 的 AI 审查意见。
OpenClaw CLI 的强大之处在于,它不仅仅是一个管理工具,更是一个自动化平台。掌握这些命令后,你会发现很多重复性工作都可以自动化完成。
作者: itech
来源: 公众号:AI 人工智能时代
本文首发于 AI 人工智能时代,转载请注明出处。
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。