PyBot/uninstall.sh

73 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
SERVICES=("PyBot" "PyBot_Web")
echo "=============================="
echo "开始卸载 PyBot 系统服务"
echo "=============================="
for SERVICE in "${SERVICES[@]}"; do
echo ""
echo ">>> 处理服务: $SERVICE"
# 1. 停止服务
if systemctl is-active --quiet $SERVICE; then
echo "停止服务..."
sudo systemctl stop $SERVICE
else
echo "服务未运行"
fi
# 2. 禁用开机自启
if systemctl is-enabled --quiet $SERVICE 2>/dev/null; then
echo "取消开机自启..."
sudo systemctl disable $SERVICE
else
echo "未启用开机自启"
fi
# 3. 删除 service 文件
SERVICE_FILE="/etc/systemd/system/${SERVICE}.service"
if [ -f "$SERVICE_FILE" ]; then
echo "删除服务文件: $SERVICE_FILE"
sudo rm -f "$SERVICE_FILE"
else
echo "未找到服务文件"
fi
# 4. 杀残留进程(保险)
echo "检查残留进程..."
PIDS=$(ps -ef | grep $SERVICE | grep -v grep | awk '{print $2}')
if [ -n "$PIDS" ]; then
echo "杀死残留进程: $PIDS"
sudo kill -9 $PIDS
else
echo "无残留进程"
fi
done
# 5. 重载 systemd
echo ""
echo "重新加载 systemd..."
sudo systemctl daemon-reload
sudo systemctl daemon-reexec
# 6. 可选清理日志
read -p "是否清理 PyBot 日志?(y/n): " CLEAN_LOG
if [ "$CLEAN_LOG" == "y" ]; then
echo "清理 journal 日志保留1天..."
sudo journalctl --vacuum-time=1d
fi
echo ""
echo "=============================="
echo "卸载完成 ✅"
echo "=============================="
# 7. 验证
for SERVICE in "${SERVICES[@]}"; do
echo ""
echo "验证服务: $SERVICE"
systemctl status $SERVICE 2>&1 | grep "could not be found" && echo "✔ 已彻底删除" || echo "⚠ 可能仍存在"
done