Files
linux-bash/mengyamonitor/uninstall_mengyamonitor.sh
2026-02-17 17:28:37 +08:00

103 lines
2.2 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.
#!/usr/bin/env bash
set -euo pipefail
# MengyaMonitor 一键卸载脚本
# 用法curl -fsSL "https://pan.shumengya.top/d/scripts/mengyamonitor/uninstall_mengyamonitor.sh" | sudo bash
INSTALL_DIR="/shumengya/bin/mengyamonitor"
SERVICE_NAME="smy-mengyamonitor"
BINARY_NAME="mengyamonitor-backend"
log() { printf '[MengyaMonitor-卸载] %s\n' "$*" >&2; }
fail() { log "错误: $*" >&2; exit 1; }
show_progress() {
local pid=$1
local text=$2
local delay=0.1
local spin='-\|/'
printf "[MengyaMonitor-卸载] %s... " "$text" >&2
while ps -p "$pid" > /dev/null 2>&1; do
local temp=${spin#?}
printf "\b%c" "$spin" >&2
local spin=$temp${spin%"$temp"}
sleep $delay
done
printf "\b完成\n" >&2
}
require_root() {
if [ "${EUID:-$(id -u)}" -ne 0 ]; then
fail "请使用 root 权限运行 (sudo bash uninstall_mengyamonitor.sh)"
fi
}
stop_and_disable_service() {
if systemctl is-active --quiet "${SERVICE_NAME}.service"; then
systemctl stop "${SERVICE_NAME}.service"
fi
if systemctl is-enabled --quiet "${SERVICE_NAME}.service" 2>/dev/null; then
systemctl disable "${SERVICE_NAME}.service"
fi
}
remove_service_file() {
local service_file="/etc/systemd/system/${SERVICE_NAME}.service"
if [ -f "$service_file" ]; then
rm -f "$service_file"
systemctl daemon-reload
fi
}
cleanup_process() {
# 查找并杀掉所有匹配的进程
local pids
pids=$(pgrep -f "$BINARY_NAME" || true)
if [ -n "$pids" ]; then
kill -9 $pids 2>/dev/null || true
fi
}
uninstall_main() {
log "正在卸载 MengyaMonitor ..."
(stop_and_disable_service) &
show_progress $! "停止服务"
cleanup_process
(remove_service_file) &
show_progress $! "删除服务文件"
if [ -f "$INSTALL_DIR/$BINARY_NAME" ]; then
rm -f "$INSTALL_DIR/$BINARY_NAME"
fi
# 清理日志文件(可选,这里默认清理)
if [ -f "/var/log/mengyamonitor.log" ]; then
rm -f "/var/log/mengyamonitor.log"
log "已删除日志文件"
fi
# 如果目录为空,则删除目录
if [ -d "$INSTALL_DIR" ]; then
rm -rf "$INSTALL_DIR"
log "已删除安装目录"
fi
log "=========================================="
log "卸载任务完成"
log "=========================================="
}
main() {
require_root
uninstall_main
}
main "$@"