auto_trade_sys/docs/SUPERVISOR_TROUBLESHOOTING.md
薇薇安 86b85c2609 a
2026-01-25 11:19:39 +08:00

220 lines
5.7 KiB
Markdown
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.

# Supervisor 交易进程启动问题排查指南
## 🔍 问题1UnicodeDecodeError编码错误
### 错误信息
```
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb0 in position 0: invalid start byte
AttributeError: type object 'Faults' has no attribute 'utf-8'
```
### 原因
Supervisor 的 XML-RPC 接口在读取日志时如果日志文件包含非UTF-8字符如中文会报编码错误。
### ✅ 已修复
- `backend/api/supervisor_account.py``tail_supervisor()` 函数已修复
- 如果 `supervisorctl tail` 遇到编码错误,会自动回退到直接读取日志文件
- `_tail_text_file()` 函数已增强支持多种编码UTF-8、GBK、GB2312等
### 验证
重新启动交易进程,编码错误应该不再出现。
---
## 🔍 问题2进程启动失败exit status 1
### 错误信息
```
2026-01-22 15:32:35,250 INFO spawned: 'auto_sys_acc4' with pid 2855641
2026-01-22 15:32:35,574 INFO success: auto_sys_acc4 entered RUNNING state
2026-01-22 15:32:36,074 INFO exited: auto_sys_acc4 (exit status 1; not expected)
```
### 排查步骤
#### 1. 查看进程错误日志
**方法A通过前端查看**
- 进入账号配置页面
- 查看"我的交易进程"部分
- 点击"查看最近错误日志stderr"
**方法B直接查看日志文件**
```bash
# 找到日志文件路径(通常在项目根目录的 logs 目录)
cd /www/wwwroot/autosys_new # 替换为你的项目路径
tail -n 200 logs/trading_4.err.log # 替换 4 为你的 account_id
```
**方法C查看 Supervisor 主日志**
```bash
# 宝塔面板常见路径
tail -n 200 /www/server/panel/plugin/supervisor/log/supervisord.log
```
#### 2. 常见原因及解决方案
##### 原因1Python 路径不可执行或依赖缺失
**症状**
```
ModuleNotFoundError: No module named 'binance_client'
/bin/python: No such file or directory
```
**解决方案**
1. 检查 Python 路径是否正确:
```bash
# 检查 supervisor 配置文件中的 python 路径
cat /www/server/panel/plugin/supervisor/profile/auto_sys_acc4.ini
# 或
cat /www/wwwroot/supervisor_gen/auto_sys_acc4.ini
```
2. 确保使用正确的 Python 环境:
```bash
# 设置环境变量(在 supervisor 配置中)
TRADING_PYTHON_BIN=/www/wwwroot/autosys_new/trading_system/.venv/bin/python
```
3. 检查依赖是否安装:
```bash
# 进入 trading_system 的虚拟环境
source /www/wwwroot/autosys_new/trading_system/.venv/bin/activate
pip list | grep binance
```
##### 原因2API 密钥未配置
**症状**
```
ERROR - 无法获取账户余额可能是API权限问题
ERROR - API密钥未配置
```
**解决方案**
1. 在账号配置页面设置 API Key 和 Secret
2. 确保 API Key 有"合约交易"权限
3. 检查 IP 白名单设置(如果设置了)
##### 原因3工作目录不存在
**症状**
```
FileNotFoundError: [Errno 2] No such file or directory
```
**解决方案**
1. 检查 supervisor 配置中的 `directory` 路径是否存在
2. 确保项目根目录路径正确
##### 原因4权限问题
**症状**
```
Permission denied
```
**解决方案**
1. 检查日志目录权限:
```bash
chmod 755 /www/wwwroot/autosys_new/logs
```
2. 检查 supervisor 运行用户:
```bash
# 在 supervisor 配置中设置
user=www # 替换为实际运行用户
```
##### 原因5配置管理器初始化失败
**症状**
```
ERROR - 配置管理器初始化失败
```
**解决方案**
1. 检查数据库连接
2. 检查 Redis 连接(如果使用)
3. 检查环境变量配置
#### 3. 手动测试启动
如果自动启动失败,可以手动测试:
```bash
# 1. 进入项目根目录
cd /www/wwwroot/autosys_new
# 2. 设置环境变量
export ATS_ACCOUNT_ID=4 # 替换为你的 account_id
export PYTHONPATH=/www/wwwroot/autosys_new
# 3. 使用正确的 Python 环境启动
/www/wwwroot/autosys_new/trading_system/.venv/bin/python -m trading_system.main
# 4. 查看错误信息
```
---
## 🔧 修复后的改进
### 1. 编码错误修复
-`tail_supervisor()` 函数现在会自动回退到直接读取文件
-`_tail_text_file()` 支持多种编码UTF-8、GBK、GB2312等
### 2. 错误诊断增强
- ✅ 启动失败时会自动读取多种日志源:
- Supervisor stderr 日志
- Supervisor stdout 日志
- 直接读取日志文件
- Supervisor 主日志
### 3. 日志编码统一
- ✅ 所有日志文件使用 UTF-8 编码
- ✅ 读取时支持多种编码回退
---
## 📋 快速检查清单
当进程启动失败时,按以下顺序检查:
1.**查看错误日志**`logs/trading_{account_id}.err.log`
2.**检查 Python 路径**supervisor 配置中的 `command` 路径是否正确
3.**检查依赖**`pip list | grep binance` 确认依赖已安装
4.**检查 API 密钥**:账号配置页面是否已设置
5.**检查权限**:日志目录和项目目录是否有读写权限
6.**检查环境变量**`ATS_ACCOUNT_ID` 是否正确设置
7.**手动测试**:使用命令行手动启动,查看详细错误
---
## 🆘 如果问题仍然存在
1. **收集信息**
- 错误日志stderr
- Supervisor 配置(.ini 文件)
- 手动启动的输出
2. **检查系统日志**
```bash
# 查看系统日志
journalctl -u supervisord -n 100
```
3. **联系支持**:提供完整的错误信息和日志
---
## 📝 相关文件
- `backend/api/supervisor_account.py` - Supervisor 管理代码
- `backend/api/routes/accounts.py` - 账号管理 API
- `trading_system/main.py` - 交易系统主程序
- `logs/trading_{account_id}.err.log` - 错误日志
- `logs/trading_{account_id}.out.log` - 标准输出日志