Appearance
OpenClaw CLI 常用命令与实用技巧
🚀 核心命令概览
系统状态与管理
bash
# 查看系统状态
openclaw status
# 查看网关状态
openclaw gateway status
# 启动网关服务
openclaw gateway start
# 停止网关服务
openclaw gateway stop
# 重启网关服务
openclaw gateway restart
# 查看配置
openclaw config get
openclaw config set <key> <value>会话与会话管理
bash
# 列出所有活跃会话
openclaw sessions list
# 查看特定会话历史
openclaw sessions history <sessionKey>
# 发送消息到其他会话
openclaw sessions send --sessionKey <key> --message "消息内容"
# 查看子代理状态
openclaw subagents list代理与技能管理
bash
# 列出可用代理
openclaw agents list
# 启动子代理会话
openclaw agents spawn --agentId <id> --task "任务描述"
# 查看已安装技能
openclaw skills list
# 安装技能
openclaw skills install <skill-name>🔧 实用技巧与最佳实践
1. 服务管理
后台运行与日志查看
bash
# 后台运行网关并查看日志
openclaw gateway start --background
tail -f ~/.openclaw/logs/gateway.log
# 检查服务健康状态
openclaw status --detailed服务调试
bash
# 带调试日志启动
DEBUG=openclaw:* openclaw gateway start
# 检查端口占用
lsof -i :187892. 配置管理
配置文件位置
~/.openclaw/config.json # 主配置文件
~/.openclaw/workspace-main/ # 主工作区目录
~/.openclaw/logs/ # 日志目录常用配置项
json
{
"gateway": {
"port": 18789,
"host": "127.0.0.1"
},
"model": {
"default": "edgefn-invest/DeepSeek-V3.2"
},
"memory": {
"enabled": true,
"path": "memory"
}
}3. 会话操作技巧
会话筛选与过滤
bash
# 按活跃时间筛选
openclaw sessions list --activeMinutes 30
# 查看特定类型会话
openclaw sessions list --kinds main,subagent
# 限制显示数量
openclaw sessions list --limit 10会话监控
bash
# 实时监控会话活动
watch -n 5 "openclaw sessions list --limit 5"
# 查看完整会话详情
openclaw sessions list --messageLimit 34. 故障排查
常见问题诊断
bash
# 检查网关连接
curl http://127.0.0.1:18789/status
# 查看错误日志
grep -i error ~/.openclaw/logs/*.log
# 验证配置
openclaw config get --all服务恢复
bash
# 强制重启服务
openclaw gateway stop --force
openclaw gateway start
# 清理临时文件
rm -rf ~/.openclaw/tmp/*
# 重建工作区
openclaw workspace init📊 监控与性能
性能指标监控
bash
# 查看内存使用
ps aux | grep openclaw
# 监控CPU使用
top -pid $(pgrep -f openclaw)
# 查看日志大小
du -sh ~/.openclaw/logs/健康检查脚本
bash
#!/bin/bash
# openclaw-health-check.sh
# 检查服务状态
if openclaw gateway status | grep -q "running"; then
echo "✅ Gateway is running"
else
echo "❌ Gateway is not running"
fi
# 检查端口
if nc -z 127.0.0.1 18789; then
echo "✅ Port 18789 is open"
else
echo "❌ Port 18789 is not responding"
fi
# 检查配置文件
if [ -f ~/.openclaw/config.json ]; then
echo "✅ Config file exists"
else
echo "❌ Config file missing"
fi🛡️ 安全最佳实践
访问控制
bash
# 限制外部访问
openclaw config set gateway.host 127.0.0.1
# 启用HTTPS(如支持)
openclaw config set gateway.https true
# 设置访问令牌
openclaw config set gateway.token $(openssl rand -hex 16)日志管理
bash
# 启用详细日志用于调试
openclaw config set logging.level debug
# 配置日志轮转
openclaw config set logging.rotation "daily"
# 清理旧日志
find ~/.openclaw/logs -name "*.log.*" -mtime +7 -delete🔄 自动化脚本
定时备份脚本
bash
#!/bin/bash
# backup-openclaw.sh
BACKUP_DIR=~/openclaw-backups
DATE=$(date +%Y%m%d-%H%M%S)
# 创建备份目录
mkdir -p $BACKUP_DIR
# 备份配置文件
cp ~/.openclaw/config.json $BACKUP_DIR/config-$DATE.json
# 备份工作区(排除node_modules)
rsync -av --exclude=node_modules ~/.openclaw/workspace-main/ $BACKUP_DIR/workspace-$DATE/
# 备份日志(保留最近7天)
tar -czf $BACKUP_DIR/logs-$DATE.tar.gz ~/.openclaw/logs/
echo "Backup completed to $BACKUP_DIR"自动重启监控
bash
#!/bin/bash
# monitor-openclaw.sh
while true; do
if ! openclaw gateway status | grep -q "running"; then
echo "[$(date)] Gateway not running, restarting..."
openclaw gateway start
fi
sleep 60
done💡 实用小贴士
别名设置
bash
# 添加到 ~/.bashrc 或 ~/.zshrc
alias ocs='openclaw status'
alias ocg='openclaw gateway'
alias ocl='openclaw sessions list'
alias ocsend='openclaw sessions send'快速命令
bash
# 快速重启网关
ocs && ocg restart
# 快速查看最近会话
ocl --limit 5 --activeMinutes 60
# 发送测试消息
ocsend --sessionKey "test" --message "测试消息"环境变量
bash
# 设置默认模型
export OPENCLAW_MODEL=deepseek-chat
# 设置工作区路径
export OPENCLAW_WORKSPACE=~/.openclaw/workspace-main
# 设置日志级别
export OPENCLAW_LOG_LEVEL=info🚨 故障排查清单
常见问题与解决方案
| 问题 | 可能原因 | 解决方案 |
|---|---|---|
| 网关无法启动 | 端口被占用 | lsof -i :18789 然后 kill <PID> |
| 会话丢失 | 内存问题 | 重启服务,检查日志 |
| 技能加载失败 | 配置错误 | 检查技能目录和权限 |
| 消息发送失败 | 网络问题 | 检查网关连接状态 |
| 权限问题 | 文件权限 | chmod -R 755 ~/.openclaw |
诊断命令流程
- 检查服务状态:
openclaw status - 查看错误日志:
tail -100 ~/.openclaw/logs/gateway.log - 验证配置:
openclaw config get --all - 测试连接:
curl http://127.0.0.1:18789/status - 检查端口:
netstat -tlnp | grep 18789
本文档基于 OpenClaw v2026.2.26+ 版本编写,建议定期更新以适配新版本特性。