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

105 lines
2.3 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
# openlist 一键卸载脚本
# 用法sudo bash uninstall_openlist.sh
INSTALL_DIR="/shumengya/bin/openlist"
SERVICE_NAME="smy-openlist"
SERVICE_FILE="/etc/systemd/system/${SERVICE_NAME}.service"
log() { printf '[openlist-卸载] %s\n' "$*" >&2; }
fail() { log "错误: $*" >&2; exit 1; }
# 进度条函数
show_progress() {
local pid=$1
local text=$2
local delay=0.1
local spin='-\|/'
printf "[openlist-卸载] %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() {
# 必须使用 root 权限执行
if [ "${EUID:-$(id -u)}" -ne 0 ]; then
fail "请使用 root 权限运行 (sudo bash uninstall_openlist.sh)"
fi
}
stop_and_disable_service() {
# 停止并禁用 systemd 服务
if systemctl is-active --quiet "$SERVICE_NAME"; then
log "正在停止服务 $SERVICE_NAME..."
systemctl stop "$SERVICE_NAME"
fi
if systemctl is-enabled --quiet "$SERVICE_NAME" 2>/dev/null; then
log "正在禁用服务 $SERVICE_NAME..."
systemctl disable "$SERVICE_NAME"
fi
}
remove_service_file() {
# 删除 systemd 服务文件
if [ -f "$SERVICE_FILE" ]; then
log "正在删除服务文件..."
rm -f "$SERVICE_FILE"
systemctl daemon-reload
fi
}
remove_install_dir() {
# 删除安装目录
if [ -d "$INSTALL_DIR" ]; then
log "正在删除安装目录 $INSTALL_DIR..."
rm -rf "$INSTALL_DIR"
fi
}
check_and_kill_port() {
local port=5244
if lsof -i :$port >/dev/null 2>&1 || netstat -tunlp | grep -q ":$port "; then
log "检测到端口 $port 仍被占用,尝试强制清理..."
fuser -k -n tcp $port >/dev/null 2>&1 || true
sleep 1
# 二次检查
local pid
pid=$(lsof -t -i:$port 2>/dev/null || true)
if [ -n "$pid" ]; then
kill -9 "$pid" 2>/dev/null || true
fi
fi
}
main() {
require_root
log "开始卸载 openlist..."
(stop_and_disable_service) &
show_progress $! "正在停止服务"
(remove_service_file) &
show_progress $! "正在删除服务文件"
check_and_kill_port
(remove_install_dir) &
show_progress $! "正在删除安装目录"
log "=========================================="
log "卸载完成! openlist 已从系统中移除"
log "=========================================="
}
main "$@"