#!/usr/bin/env bash # 通过 pidfile 优雅停止 SFTP 与 HTTP,超时则 SIGKILL。 set -uo pipefail cd "$(dirname "$0")" ROOT="$(pwd)" stop_pidfile() { local name="$1" pidfile="$2" if [ ! -f "$pidfile" ]; then echo "$name: 无 pidfile ($pidfile),未运行。" return 0 fi local pid; pid="$(cat "$pidfile" 2>/dev/null || true)" if [ -z "$pid" ] || ! kill -0 "$pid" 2>/dev/null; then rm -f "$pidfile" echo "$name: 进程已退出,清理 pidfile。" return 0 fi echo "$name: SIGTERM -> $pid ..." kill -TERM "$pid" 2>/dev/null || true for _ in $(seq 1 20); do kill -0 "$pid" 2>/dev/null || break sleep 0.25 done if kill -0 "$pid" 2>/dev/null; then echo "$name: 仍在运行,发送 SIGKILL..." kill -KILL "$pid" 2>/dev/null || true fi rm -f "$pidfile" echo "$name: 已停止。" } # 先停 SFTP(避免拆解过程中又有新文件落盘),再停 HTTP。 stop_pidfile "SFTP" "$ROOT/sftp.pid" stop_pidfile "HTTP" "$ROOT/app.pid" echo "全部服务已停止。"