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

94 lines
2.1 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
# 获取当前目录
BASE_DIR=$(cd "$(dirname "$0")"; pwd)
USER_NAME=$(whoami)
echo "项目路径: $BASE_DIR"
# ========= 自动检测 Python =========
detect_python() {
if command -v python >/dev/null 2>&1; then
PY=python
elif command -v python3 >/dev/null 2>&1; then
PY=python3
else
echo "❌ 未检测到 Python请先安装 Python 3.6+"
exit 1
fi
# 获取版本号
VERSION=$($PY -c 'import sys; print(".".join(map(str, sys.version_info[:3])))')
echo "检测到 Python: $PY ($VERSION)"
# 校验版本 >= 3.6
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"
exit 1
fi
PYTHON_CMD=$PY
}
detect_python
# ========= 创建 PyBot 服务 =========
sudo bash -c "cat > /etc/systemd/system/PyBot.service" <<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
StartLimitInterval=0
Environment=PYTHONUNBUFFERED=1
[Install]
WantedBy=multi-user.target
EOF
# ========= 创建 PyBot_Web 服务 =========
sudo bash -c "cat > /etc/systemd/system/PyBot_Web.service" <<EOF
[Unit]
Description=PyBot Web Service
After=network.target
[Service]
Type=simple
User=$USER_NAME
WorkingDirectory=$BASE_DIR
ExecStart=$PYTHON_CMD $BASE_DIR/web/app.py
Restart=always
RestartSec=3
StartLimitInterval=0
Environment=PYTHONUNBUFFERED=1
[Install]
WantedBy=multi-user.target
EOF
# ========= 重新加载 =========
sudo systemctl daemon-reexec
sudo systemctl daemon-reload
# ========= 启用 + 启动 =========
sudo systemctl enable PyBot
sudo systemctl enable PyBot_Web
sudo systemctl restart PyBot
sudo systemctl restart PyBot_Web
echo "===================================="
echo "✅ 服务已创建并启动"
echo "PyBot → core.py"
echo "PyBot_Web → web/app.py"
echo "===================================="