Files
opendance-web/deploy/uninstall.sh
T
zijunchiang c092b833fb fix(deploy): 支持无 systemd 的环境,并修复降级路径的两个真实缺陷
用户实测:服务器没有 systemctl,install.sh 直接报错退出。

原错误信息本身也是错的(建议 SKIP_NGINX=1,但那只跳过 nginx,仍走 systemctl)。

进程管理改为四层降级:
  systemd → OpenRC → SysVinit → nohup 后台进程(deploy/ctl.sh)
- 新增 deploy/ctl.sh:start/stop/restart/status/logs,PID 文件 + 进程名双重判定
- 新增 deploy/openrc/opendance-api、deploy/initd/opendance-api
- 无 init 系统时写 crontab @reboot 实现开机自启
- uninstall.sh 同步支持四种模式清理(原先只处理 systemd,会留下运行中的进程)
- 汇总输出按实际初始化系统给出对应运维命令

测试中发现并修复降级路径的两个真实缺陷:

1. nohup 无法调用 shell 函数
    实测退出码 127 且什么都不执行
   (nohup 是外部命令,只能执行可执行文件)。
   这会让无 systemd 的降级路径完全失效——服务根本起不来。
   改为按 runuser / setpriv / su 显式拼接成外部命令后再 nohup。
   另加启动后 1 秒存活检测,失败立即打印日志,不再白等 30 秒。

2. find_pids 在无进程时返回一个空格
    在空输入下仍会执行一次 echo,输出空行,
   最终得到 " ";调用方 [ -n ... ] 会判为「有进程」,
   导致 start 直接跳过、status 谎报运行中。改为纯 bash 拼接,空时返回空串。

实测(真实 API 进程,端口 3100):
  start → 健康检查 200,PID 正确
  重复 start → 识别已在运行,进程数保持 1
  status → 运行中
  restart → 新 PID,健康检查 200
  stop → 无残留进程,端口释放
  无 process 时 find_pids 返回空串,is_running=false
2026-09-15 00:34:52 +08:00

77 lines
3.4 KiB
Bash
Executable File

#!/bin/bash
# ============================================================
# OpenDance Studio Web 服务端 —— 卸载(Linux 原生站点)
#
# 默认**保留**上传文件与数据库数据;只有显式加 --purge 才删除程序目录与上传目录。
# 数据库从来不由本脚本管理,永不删除。
#
# 用法:
# bash deploy/uninstall.sh # 停服务、禁用站点,保留数据(需 root)
# bash deploy/uninstall.sh --purge # 同时删除程序目录与上传文件
# ============================================================
set -euo pipefail
APP_DIR="${APP_DIR:-/opt/opendance}"
STORAGE_DIR="${STORAGE_DIR:-/var/lib/opendance/storage}"
SERVICE_USER="${SERVICE_USER:-opendance}"
PURGE=0
[ "${1:-}" = "--purge" ] && PURGE=1
log() { printf '\033[1;34m[uninstall]\033[0m %s\n' "$*"; }
warn() { printf '\033[1;33m[warn]\033[0m %s\n' "$*"; }
die() { printf '\033[1;31m[error]\033[0m %s\n' "$*" >&2; exit 1; }
[ "$(id -u)" -eq 0 ] || die "需要 root 权限。请以 root 身份执行,例如:
su -c 'bash deploy/uninstall.sh' 或 sudo bash deploy/uninstall.sh"
log "停止并移除 opendance-api 服务"
# 与 install.sh 的四层降级一一对应,避免在非 systemd 系统上留下仍在运行的进程
if [ -d /run/systemd/system ] && command -v systemctl >/dev/null 2>&1; then
systemctl stop opendance-api 2>/dev/null || true
systemctl disable opendance-api 2>/dev/null || true
rm -f /etc/systemd/system/opendance-api.service
systemctl daemon-reload
elif command -v rc-service >/dev/null 2>&1 && [ -f /etc/init.d/opendance-api ]; then
rc-service opendance-api stop 2>/dev/null || true
rc-update del opendance-api default 2>/dev/null || true
rm -f /etc/init.d/opendance-api
elif [ -f /etc/init.d/opendance-api ]; then
/etc/init.d/opendance-api stop 2>/dev/null || true
command -v update-rc.d >/dev/null 2>&1 && update-rc.d -f opendance-api remove 2>/dev/null || true
command -v chkconfig >/dev/null 2>&1 && chkconfig --del opendance-api 2>/dev/null || true
rm -f /etc/init.d/opendance-api
fi
# 降级模式(nohup 后台进程)的停止与开机自启清理
if [ -f "$APP_DIR/deploy/ctl.sh" ]; then
APP_DIR="$APP_DIR" SERVICE_USER="$SERVICE_USER" bash "$APP_DIR/deploy/ctl.sh" stop 2>/dev/null || true
fi
if command -v crontab >/dev/null 2>&1; then
if crontab -l 2>/dev/null | grep -qF "$APP_DIR/deploy/ctl.sh start"; then
crontab -l 2>/dev/null | grep -vF "$APP_DIR/deploy/ctl.sh start" | crontab -
log "已移除 crontab 开机自启"
fi
fi
if command -v nginx >/dev/null 2>&1; then
log "移除 nginx 站点"
rm -f /etc/nginx/sites-enabled/opendance.conf /etc/nginx/sites-available/opendance.conf /etc/nginx/conf.d/opendance.conf
nginx -t >/dev/null 2>&1 && (systemctl reload nginx 2>/dev/null || nginx -s reload) || warn "nginx 重载跳过"
fi
if [ "$PURGE" -eq 1 ]; then
warn "删除程序目录 $APP_DIR 与上传目录 $STORAGE_DIR"
rm -rf "$APP_DIR" "$STORAGE_DIR"
if id -u "$SERVICE_USER" >/dev/null 2>&1; then
userdel "$SERVICE_USER" 2>/dev/null || true
log "已删除系统用户 $SERVICE_USER"
fi
log "完成(数据库未做任何改动)"
else
log "完成。已保留:"
echo " 程序目录:$APP_DIR"
echo " 上传目录:$STORAGE_DIR"
echo " 如需一并删除(root 身份):bash deploy/uninstall.sh --purge"
fi
echo " 数据库:始终由部署方自行管理,本脚本不做任何改动"