PyBot/install.sh
2026-05-20 00:40:03 +08:00

75 lines
1.8 KiB
Bash
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
set -e
BASE_DIR=$(cd "$(dirname "$0")"; pwd)
USER_NAME=$(whoami)
echo "项目路径: $BASE_DIR"
# 检测 Python
if command -v python3 >/dev/null 2>&1; then
PYTHON_CMD=python3
elif command -v python >/dev/null 2>&1; then
PYTHON_CMD=python
else
echo "❌ 未检测到 Python请先安装 Python 3.6+"
exit 1
fi
VERSION=$($PYTHON_CMD -c 'import sys; print(".".join(map(str, sys.version_info[:3])))')
MAJOR=$(echo $VERSION | cut -d. -f1)
MINOR=$(echo $VERSION | cut -d. -f2)
if [ "$MAJOR" -lt 3 ] || { [ "$MAJOR" -eq 3 ] && [ "$MINOR" -lt 6 ]; }; then
echo "❌ Python 版本过低(需要 >= 3.6),当前版本: $VERSION"
exit 1
fi
echo "✅ 检测到 Python: $PYTHON_CMD ($VERSION)"
# 检查 pip
if ! $PYTHON_CMD -m pip --version >/dev/null 2>&1; then
echo "❌ pip 未安装或不可用"
exit 1
fi
# 安装依赖
if [ -f "$BASE_DIR/requirements.txt" ]; then
echo "📦 安装 Python 依赖..."
$PYTHON_CMD -m pip install --upgrade pip -q
$PYTHON_CMD -m pip install -r "$BASE_DIR/requirements.txt" -q
echo "✅ 依赖安装完成"
else
echo "⚠️ 未找到 requirements.txt跳过依赖安装"
fi
# 创建 systemd 服务
sudo tee /etc/systemd/system/PyBot.service > /dev/null <<EOF
[Unit]
Description=PyBot Core Service
After=network.target
[Service]
Type=simple
User=$USER_NAME
WorkingDirectory=$BASE_DIR
ExecStart=$PYTHON_CMD $BASE_DIR/Core.py
Restart=always
RestartSec=3
Environment=PYTHONUNBUFFERED=1
[Install]
WantedBy=multi-user.target
EOF
# 重载并启动服务
sudo systemctl daemon-reload
sudo systemctl enable --now PyBot
echo ""
echo "===================================="
echo "✅ PyBot 服务已安装并启动"
echo "📄 日志查看: sudo journalctl -u PyBot -f"
echo "🌐 访问 index.php 获取网页服务"
echo "===================================="