sage/stop.sh
yumoqing 53285aa17e feat: multi-process worker scaling for Sage web app
- Refactor start.sh to launch multiple worker processes based on CPU core count
- Assign dynamic ports to each worker (base_port + offset)
- Update stop.sh to gracefully handle and kill multiple worker PIDs
- Implement PID file management for multi-process tracking
2026-05-16 22:08:52 +08:00

80 lines
2.0 KiB
Bash
Executable File

#!/bin/bash
# Sage Web Application Stop Script
# Supports multi-process setup
set -e
# 切换到脚本所在目录
cd "$(dirname "$0")"
WORKDIR="$(pwd)"
PIDFILE="$WORKDIR/sage.pid"
echo "========================================="
echo "停止 Sage Web Application"
echo "========================================="
STOPPED_PIDS=""
# 1. 尝试从 PID 文件停止
if [ -f "$PIDFILE" ]; then
echo "读取 PID 文件..."
while read -r APP_PID; do
# 跳过空行
if [ -z "$APP_PID" ]; then continue; fi
if kill -0 "$APP_PID" 2>/dev/null; then
echo "正在停止 Worker (PID: $APP_PID) ..."
kill "$APP_PID" 2>/dev/null || true
STOPPED_PIDS="$STOPPED_PIDS $APP_PID"
else
echo "Worker (PID: $APP_PID) 已停止"
fi
done < "$PIDFILE"
# 等待进程结束
WAIT_COUNT=0
while [ $WAIT_COUNT -lt 10 ]; do
ALL_STOPPED=true
for PID in $STOPPED_PIDS; do
if kill -0 "$PID" 2>/dev/null; then
ALL_STOPPED=false
break
fi
done
if $ALL_STOPPED; then
break
fi
sleep 1
WAIT_COUNT=$((WAIT_COUNT + 1))
echo "等待服务关闭... ($WAIT_COUNT/10)"
done
# 强制杀死未退出的
for PID in $STOPPED_PIDS; do
if kill -0 "$PID" 2>/dev/null; then
echo "强制停止进程: $PID"
kill -9 "$PID" 2>/dev/null || true
fi
done
fi
# 2. 兜底清理 (通过进程名查找,防止 PID 文件丢失)
# 注意:这里匹配 app/sage.py
PIDS=$(ps aux | grep "[a]pp/sage.py" | awk '{print $2}' || true)
if [ -n "$PIDS" ]; then
echo "发现残留进程,强制清理..."
for PID in $PIDS; do
kill -9 "$PID" 2>/dev/null || true
done
fi
# 清理 PID 文件
rm -f "$PIDFILE"
echo "========================================="
echo "服务已停止"
echo "========================================="