jameshuntercarter/Ace-Step-v1.5
0
1#!/usr/bin/env bash2set -euo pipefail3 4usage() {5 cat <<'EOF'6Usage:7 ./close_api_server.sh [--port PORT] [--pid PID] [--force]8 9Defaults:10 PORT: 800111 12Behavior:13 - If --pid is provided, stops that PID.14 - Otherwise, finds the listening PID(s) on --port and stops them.15 - By default, only stops processes whose cmdline contains "uvicorn" or "acestep.api_server".16 Use --force to skip this safety check.17EOF18}19 20PORT="8001"21PID=""22FORCE="0"23 24while [[ $# -gt 0 ]]; do25 case "$1" in26 --port)27 PORT="${2:-}"; shift 2 ;;28 --pid)29 PID="${2:-}"; shift 2 ;;30 --force)31 FORCE="1"; shift ;;32 -h|--help)33 usage; exit 0 ;;34 *)35 echo "Unknown argument: $1" >&236 usage37 exit 238 ;;39 esac40done41 42if [[ -n "$PORT" ]] && ! [[ "$PORT" =~ ^[0-9]+$ ]]; then43 echo "Invalid --port: $PORT" >&244 exit 245fi46if [[ -n "$PID" ]] && ! [[ "$PID" =~ ^[0-9]+$ ]]; then47 echo "Invalid --pid: $PID" >&248 exit 249fi50 51_cmdline() {52 local pid="$1"53 if [[ -r "/proc/${pid}/cmdline" ]]; then54 tr '\0' ' ' < "/proc/${pid}/cmdline" | sed 's/[[:space:]]\+/ /g' || true55 else56 echo ""57 fi58}59 60_is_target_process() {61 local pid="$1"62 local cmd63 cmd="$(_cmdline "$pid")"64 [[ "$cmd" == *"uvicorn"* || "$cmd" == *"acestep.api_server"* ]]65}66 67_find_pids_by_port() {68 local port="$1"69 local pids=""70 71 if command -v lsof >/dev/null 2>&1; then72 pids="$(lsof -nP -t -iTCP:"$port" -sTCP:LISTEN 2>/dev/null | tr '\n' ' ' || true)"73 elif command -v ss >/dev/null 2>&1; then74 # 输出示例:LISTEN 0 4096 127.0.0.1:8001 ... users:("python",pid=12345,fd=3)75 pids="$(ss -lptn "sport = :$port" 2>/dev/null | sed -n 's/.*pid=\([0-9]\+\).*/\1/p' | sort -u | tr '\n' ' ' || true)"76 elif command -v netstat >/dev/null 2>&1; then77 # 输出示例:tcp ... LISTEN 12345/python78 pids="$(netstat -lntp 2>/dev/null | awk -v p=":${port}" '$4 ~ p && $6=="LISTEN" {split($7,a,"/"); if (a[1] ~ /^[0-9]+$/) print a[1]}' | sort -u | tr '\n' ' ' || true)"79 elif command -v fuser >/dev/null 2>&1; then80 pids="$(fuser -n tcp "$port" 2>/dev/null | tr '\n' ' ' || true)"81 fi82 83 echo "$pids"84}85 86_stop_pid() {87 local pid="$1"88 89 if ! kill -0 "$pid" 2>/dev/null; then90 echo "PID $pid not running."91 return 092 fi93 94 if [[ "$FORCE" != "1" ]] && ! _is_target_process "$pid"; then95 echo "Skip PID $pid (cmdline does not look like uvicorn/acestep.api_server). Use --force to stop anyway." >&296 echo "cmdline: $(_cmdline "$pid")" >&297 return 398 fi99 100 echo "Stopping PID $pid..."101 kill -TERM "$pid" 2>/dev/null || true102 103 for _ in $(seq 1 30); do104 if ! kill -0 "$pid" 2>/dev/null; then105 echo "Stopped PID $pid."106 return 0107 fi108 sleep 0.2109 done110 111 echo "PID $pid did not exit; sending SIGKILL..." >&2112 kill -KILL "$pid" 2>/dev/null || true113 sleep 0.1114 if kill -0 "$pid" 2>/dev/null; then115 echo "Failed to kill PID $pid." >&2116 return 1117 fi118 echo "Killed PID $pid."119 return 0120}121 122if [[ -n "$PID" ]]; then123 _stop_pid "$PID"124 exit $?125fi126 127pids="$(_find_pids_by_port "$PORT")"128if [[ -z "${pids// }" ]]; then129 echo "No listening process found on port $PORT."130 exit 0131fi132 133rc=0134for pid in $pids; do135 if [[ -n "$pid" ]]; then136 _stop_pid "$pid" || rc=$?137 fi138done139 140exit "$rc"141 