#!/usr/bin/env bash # PM2 一键部署 — 安装目录 /opt/Binance_Altcoin_Monitor set -euo pipefail INSTALL_DIR="/opt/Binance_Altcoin_Monitor" REPO_URL="${REPO_URL:-https://git.bz121.com/dekun/Binance_Altcoin_Monitor.git}" echo "==> PM2 部署 Binance Altcoin Monitor" echo " 目录: ${INSTALL_DIR}" if ! command -v python3 &>/dev/null; then echo "错误: 未安装 python3" exit 1 fi if ! command -v pm2 &>/dev/null; then echo "==> 安装 PM2..." if command -v npm &>/dev/null; then sudo npm install -g pm2 else echo "错误: 请先安装 Node.js/npm,或手动: npm install -g pm2" exit 1 fi fi if [ ! -d "${INSTALL_DIR}/.git" ]; then echo "==> 克隆仓库..." sudo mkdir -p /opt sudo git clone "${REPO_URL}" "${INSTALL_DIR}" sudo chown -R "$(whoami):$(whoami)" "${INSTALL_DIR}" 2>/dev/null || true else echo "==> 更新代码..." cd "${INSTALL_DIR}" git pull --rebase fi cd "${INSTALL_DIR}" if [ ! -f .env ]; then echo "==> 创建 .env" cp .env.example .env fi mkdir -p data logs echo "==> 安装 Python 依赖..." python3 -m pip install -U pip -q python3 -m pip install -r backend/requirements.txt -q echo "==> 启动 PM2..." pm2 delete binance-altcoin-monitor 2>/dev/null || true pm2 start ecosystem.config.cjs pm2 save # 开机自启(可选) if command -v pm2 &>/dev/null; then pm2 startup 2>/dev/null | tail -1 | grep -q sudo && \ echo "提示: 若需开机自启,请执行上一条 pm2 startup 输出的 sudo 命令" || true fi echo "" echo "部署完成。" echo " Web: http://$(hostname -I | awk '{print $1}'):21450" echo " 状态: pm2 status" echo " 日志: pm2 logs binance-altcoin-monitor" echo "" echo "开启 SOCKS5 代理: 编辑 ${INSTALL_DIR}/.env 后执行" echo " pm2 restart binance-altcoin-monitor"