Files
my-project/tools/xiufu_openclaw_enhanced.sh
T

243 lines
7.4 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
echo "=== OpenClaw增强修复 ==="
echo "修复时间: $(date)"
echo ""
# 1. 备份当前状态
echo "1. 📊 备份当前状态..."
CONFIG_FILE="/root/.openclaw/openclaw.json"
BACKUP_DIR="/root/.openclaw/backup"
mkdir -p "$BACKUP_DIR"
# 备份配置文件
if [ -f "$CONFIG_FILE" ]; then
BACKUP_FILE="${BACKUP_DIR}/openclaw.json.backup_$(date +%Y%m%d_%H%M%S)"
cp "$CONFIG_FILE" "$BACKUP_FILE"
echo " 配置文件备份到: $BACKUP_FILE"
fi
# 备份进程信息
echo " 当前进程:"
ps aux | grep -E "(openclaw|claw)" | grep -v grep || echo " 无相关进程"
# 2. 清理无效配置字段
echo ""
echo "2. 🧹 清理无效配置字段..."
if [ -f "$CONFIG_FILE" ]; then
echo " 运行配置修复..."
openclaw doctor --fix 2>&1 | grep -v "autoSelectFamily" | head -20
# 如果doctor失败,手动清理
if openclaw config validate 2>&1 | grep -q "invalid"; then
echo " 手动清理无效字段..."
python3 << 'PYEOF'
import json
import os
import sys
config_file = '/root/.openclaw/openclaw.json'
if not os.path.exists(config_file):
print("配置文件不存在")
sys.exit(0)
with open(config_file, 'r') as f:
config = json.load(f)
# 清理已知无效字段
invalid_fields = []
def clean_invalid(obj, path=''):
if isinstance(obj, dict):
keys_to_remove = []
for key in list(obj.keys()):
current_path = f'{path}.{key}' if path else key
# 检查Telegram相关无效字段
if 'telegram' in str(key).lower():
if 'capabilities' in str(key).lower():
if isinstance(obj[key], dict):
# 只保留有效的capabilities
valid_caps = {}
if 'inlineButtons' in obj[key]:
valid_caps['inlineButtons'] = obj[key]['inlineButtons']
obj[key] = valid_caps
elif 'defaultTarget' in str(key).lower():
# 记录但暂时保留
invalid_fields.append(current_path)
elif 'polling' in str(key).lower():
invalid_fields.append(current_path)
# 递归清理
if isinstance(obj[key], (dict, list)):
clean_invalid(obj[key], current_path)
# 移除明确无效的字段
for key in ['defaultTarget', 'polling']:
if key in obj:
del obj[key]
invalid_fields.append(f'{path}.{key}' if path else key)
elif isinstance(obj, list):
for i, item in enumerate(obj):
clean_invalid(item, f'{path}[{i}]')
clean_invalid(config)
if invalid_fields:
print(f" 清理了 {len(invalid_fields)} 个无效字段:")
for field in invalid_fields[:10]: # 只显示前10个
print(f" - {field}")
# 确保有基本的Telegram配置
if 'channels' not in config:
config['channels'] = {}
if 'telegram' not in config['channels']:
config['channels']['telegram'] = {
'enabled': True,
'token': '8529719199:AAGjLDflvMXAEdgrBTSBEczOoSMybLU4gcs'
}
elif 'enabled' not in config['channels']['telegram']:
config['channels']['telegram']['enabled'] = True
# 保存清理后的配置
with open(config_file, 'w') as f:
json.dump(config, f, indent=2, ensure_ascii=False)
print("✅ 配置清理完成")
PYEOF
fi
fi
# 3. 停止所有相关进程
echo ""
echo "3. 🛑 停止所有OpenClaw进程..."
pkill -f "openclaw" 2>/dev/null
sleep 2
# 确保进程停止
if pgrep -f "openclaw" > /dev/null; then
echo " 强制停止剩余进程..."
pkill -9 -f "openclaw" 2>/dev/null
sleep 1
fi
# 4. 清理锁文件和临时文件
echo ""
echo "4. 🗑️ 清理临时文件..."
find /root/.openclaw -name "*.lock" -delete 2>/dev/null
find /tmp -name "*openclaw*" -type f -delete 2>/dev/null 2>/dev/null
LOCK_COUNT=$(find /root/.openclaw -name "*.lock" 2>/dev/null | wc -l)
echo " 剩余锁文件: $LOCK_COUNT"
# 5. 启动网关
echo ""
echo "5. 🚀 启动网关..."
LOG_FILE="/tmp/openclaw_restart_$(date +%Y%m%d_%H%M%S).log"
echo " 日志文件: $LOG_FILE"
# 启动网关并记录日志
openclaw gateway --force --port 18789 > "$LOG_FILE" 2>&1 &
GATEWAY_PID=$!
echo " 网关PID: $GATEWAY_PID"
# 6. 等待启动
echo ""
echo "6. ⏳ 等待网关启动..."
START_SUCCESS=false
for i in {1..20}; do
# 检查进程是否存在
if ! ps -p "$GATEWAY_PID" > /dev/null 2>&1; then
echo " ❌ 第${i}秒: 网关进程已退出"
echo " 查看日志:"
tail -20 "$LOG_FILE"
break
fi
# 检查端口监听
if netstat -tlnp 2>/dev/null | grep -q ":18789 "; then
# 检查Web服务
if curl -s http://localhost:18789/ > /dev/null 2>&1; then
echo " ✅ 第${i}秒: 网关启动成功"
START_SUCCESS=true
break
fi
fi
if [ $i -le 10 ]; then
echo " ⏳ 第${i}秒: 启动中..."
fi
sleep 1
done
# 7. 验证修复
echo ""
echo "7. ✅ 验证修复..."
if [ "$START_SUCCESS" = true ]; then
echo " a) 检查网关状态:"
echo " 进程: $(ps -p $GATEWAY_PID -o pid,cmd --no-headers 2>/dev/null || echo '无')"
echo " 端口: $(netstat -tlnp 2>/dev/null | grep 18789 | wc -l)个监听"
echo ""
echo " b) 测试基本功能:"
# 测试Web访问
HTTP_STATUS=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:18789/ 2>/dev/null || echo "无法连接")
echo " Web服务: HTTP $HTTP_STATUS"
echo ""
echo " c) 测试Telegram发送:"
echo " 测试发送消息到Telegram..."
TELEGRAM_TEST=$(timeout 15 openclaw message send --channel telegram --target "5117446326" --message "修复测试 $(date '+%H:%M:%S')" 2>&1 | tail -5)
if echo "$TELEGRAM_TEST" | grep -q "✅\|Sent"; then
echo " ✅ Telegram测试成功"
else
echo " ℹ️ Telegram测试结果:"
echo "$TELEGRAM_TEST" | sed 's/^/ /'
fi
echo ""
echo " d) 系统状态:"
openclaw channels status 2>&1 | grep -A 5 -B 2 "Telegram" || echo " Telegram状态检查失败"
else
echo " ❌ 网关启动失败"
echo " 最后20行日志:"
tail -20 "$LOG_FILE"
# 尝试备用启动方式
echo ""
echo " 尝试备用启动方式..."
openclaw gateway stop 2>/dev/null || true
sleep 2
nohup openclaw gateway --port 18789 > /tmp/openclaw_fallback.log 2>&1 &
sleep 5
if curl -s http://localhost:18789/ > /dev/null 2>&1; then
echo " ✅ 备用启动方式成功"
else
echo " ❌ 所有启动方式都失败"
fi
fi
# 8. 生成修复报告
echo ""
echo "8. 📋 修复报告:"
echo "========================================"
echo "修复时间: $(date)"
echo "配置文件: $CONFIG_FILE"
echo "网关状态: $([ "$START_SUCCESS" = true ] && echo "✅ 运行中" || echo "❌ 失败")"
echo "Telegram: $(openclaw channels status 2>&1 | grep -q "Telegram.*enabled" && echo "✅ 已启用" || echo "❌ 未启用")"
echo "锁文件数: $LOCK_COUNT"
echo "========================================"
echo ""
echo "🔧 后续操作建议:"
echo " 1. 发送消息: openclaw message send --channel telegram --target 5117446326 --message '内容'"
echo " 2. 查看状态: openclaw channels status"
echo " 3. 查看日志: tail -f $LOG_FILE"
echo " 4. 如果还有问题,查看备份配置: $BACKUP_FILE"
echo ""
echo "🎉 修复完成!"