trading.ai/start_crypto_scanner.sh
2025-11-16 10:49:52 +08:00

97 lines
2.9 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
# 加密货币市场扫描启动脚本 (Docker定时任务版本)
# 用于在Docker容器中启动定时任务服务
# 设置错误时退出
set -e
# 脚本所在目录
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
# 打印启动信息
echo "=========================================="
echo "🚀 加密货币市场扫描定时服务启动"
echo "=========================================="
echo "工作目录: $(pwd)"
echo "Python版本: $(python --version 2>&1)"
echo "时区: $(date '+%Z %z')"
echo "当前时间: $(date '+%Y-%m-%d %H:%M:%S')"
echo "=========================================="
# 检查Python是否安装
if ! command -v python &> /dev/null; then
echo "❌ 错误: 未找到Python"
exit 1
fi
# 检查加密货币扫描器是否存在
if [ ! -f "crypto_scanner.py" ]; then
echo "❌ 错误: 未找到crypto_scanner.py"
exit 1
fi
# 检查依赖包是否安装
echo "📦 检查依赖包..."
if ! python -c "import binance" 2>/dev/null; then
echo "⚠️ 警告: python-binance未安装正在安装..."
pip install -r requirements.txt
fi
# 创建日志目录
mkdir -p logs
# 检查cron服务
if ! command -v cron &> /dev/null; then
echo "⚠️ 警告: cron未安装正在安装..."
apt-get update && apt-get install -y cron
fi
# 安装加密货币扫描定时任务
echo "📅 安装加密货币扫描定时任务..."
if [ -f "crontab/crypto-scanner" ]; then
# 复制定时任务配置
cp crontab/crypto-scanner /etc/cron.d/crypto-scanner
chmod 0644 /etc/cron.d/crypto-scanner
echo "✅ 加密货币扫描定时任务已安装"
else
echo "❌ 错误: 未找到crontab/crypto-scanner文件"
exit 1
fi
# 启动cron服务
echo "🔄 启动cron定时服务..."
service cron start
# 显示当前的定时任务
echo "📋 当前定时任务列表:"
crontab -l -u root 2>/dev/null || echo "暂无定时任务"
echo ""
echo "=========================================="
echo "✅ 加密货币市场扫描定时服务启动完成"
echo "=========================================="
echo "📅 定时任务计划:"
echo " - 每天 08:00: 扫描100个交易对 (隔夜行情)"
echo " - 每天 16:00: 扫描100个交易对 (下午行情)"
echo " - 每天 00:00: 扫描100个交易对 (晚间行情)"
echo " - 每周日 10:00: 扫描200个交易对 (深度扫描)"
echo ""
echo "📊 日志文件: /app/logs/crypto_cron.log"
echo "🔄 服务状态: $(service cron status | head -1)"
echo "=========================================="
# 保持容器运行监控cron服务
echo "🔍 监控cron服务状态..."
while true; do
# 检查cron服务是否运行
if ! service cron status > /dev/null 2>&1; then
echo "⚠️ cron服务停止正在重启..."
service cron start
fi
# 每小时输出一次状态
sleep 3600
echo "$(date '+%Y-%m-%d %H:%M:%S') - 加密货币扫描定时服务正常运行"
done