Files
ewoooc/kill_old_gunicorn.sh
ogt 1b4f3a7bbe
Some checks failed
CD Pipeline / deploy (push) Failing after 59s
feat: EwoooC 初始化 — 完整專案推版至 Gitea
- 建立 Gitea Actions CD pipeline (.gitea/workflows/cd.yaml)
- 部署模式: rsync Python 檔案至 188 → docker restart (volume mount)
- Dockerfile/requirements 變動時自動重建 Docker image
- 部署通知: Telegram (開始/成功/失敗)
- 健康檢查: https://mo.wooo.work/health (最多 5 次重試)
- 同步最新 CLAUDE.md / ADR-008 / memory (2026-04-19)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-19 01:21:13 +08:00

84 lines
2.0 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.
#!/bin/bash
# 清理舊的 gunicorn momo 進程(改進版,增加超時和錯誤處理)
# V-Fix 2026-01-14: 修復在高負載下卡住的問題
set -e # 遇到錯誤立即退出
# 設定超時時間(秒)
TIMEOUT=10
MAX_ATTEMPTS=3
# 日誌函數
log() {
echo "[$(/usr/bin/date '+%Y-%m-%d %H:%M:%S')] $1"
}
log "開始清理舊的 gunicorn 進程"
# 查找所有 gunicorn 進程(排除當前腳本)
PIDS=$(/usr/bin/pgrep -f 'gunicorn.*app:app' || true)
if [ -z "$PIDS" ]; then
log "沒有發現舊的 gunicorn 進程"
exit 0
fi
log "發現以下 gunicorn 進程: $PIDS"
# 嘗試優雅終止SIGTERM
for attempt in $(/usr/bin/seq 1 $MAX_ATTEMPTS); do
log "嘗試 #$attempt: 發送 SIGTERM 信號"
for pid in $PIDS; do
if /usr/bin/ps -p "$pid" > /dev/null 2>&1; then
/usr/bin/kill -TERM "$pid" 2>/dev/null || true
fi
done
# 等待進程退出
WAIT_TIME=0
ALL_KILLED=true
while [ $WAIT_TIME -lt $TIMEOUT ]; do
ALL_KILLED=true
for pid in $PIDS; do
if /usr/bin/ps -p "$pid" > /dev/null 2>&1; then
ALL_KILLED=false
break
fi
done
if [ "$ALL_KILLED" = true ]; then
log "所有進程已優雅退出"
exit 0
fi
/usr/bin/sleep 1
WAIT_TIME=$((WAIT_TIME + 1))
done
log "部分進程未能在 ${TIMEOUT} 秒內退出,重試..."
done
# 如果優雅終止失敗強制終止SIGKILL
log "優雅終止失敗,使用 SIGKILL 強制終止"
for pid in $PIDS; do
if ps -p "$pid" > /dev/null 2>&1; then
log "強制終止進程 $pid"
/usr/bin/kill -9 "$pid" 2>/dev/null || true
fi
done
# 最後確認
/usr/bin/sleep 2
REMAINING=$(/usr/bin/pgrep -f 'gunicorn.*app:app' | /usr/bin/wc -l)
if [ "$REMAINING" -gt 0 ]; then
log "警告:仍有 $REMAINING 個 gunicorn 進程殘留"
exit 1
else
log "所有 gunicorn 進程已清理完成"
exit 0
fi