fix: 8001 端口冲突修复脚本,防止双开 gunicorn 卡死全站

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
XingQue
2026-07-09 05:10:39 +08:00
parent a48c9d3134
commit ad63b3f1ee
3 changed files with 71 additions and 2 deletions

View File

@@ -27,6 +27,12 @@ NG=$(pgrep -cf 'gunicorn.*a_long_dianjing' 2>/dev/null || echo 0)
if [ "$NG" -gt 8 ]; then
echo ">>> 警告: gunicorn 进程过多($NG),可能 systemd + 1Panel 双开,必须只留 systemd"
fi
RECENT=$(grep -a 'Connection in use' /tmp/gunicorn-error.log 2>/dev/null | tail -1)
if [ -n "$RECENT" ]; then
echo ">>> 警告: error.log 曾出现 8001 端口冲突(双开 gunicorn:"
echo " $RECENT"
echo " 执行: bash deploy/fix-gunicorn-8001-conflict.sh"
fi
if grep -q 'worker_class.*sync' "$CONF" 2>/dev/null; then
echo ">>> 致命: gunicorn.conf.py 仍是 sync全站会卡死必须改为 gthread 并 restart"
fi

View File

@@ -0,0 +1,58 @@
#!/bin/bash
# 修复 8001 端口冲突:多个 Gunicorn 同时抢 8001 会导致全站卡死
# 日志特征: Connection in use: ('127.0.0.1', 8001)
# 用法: bash deploy/fix-gunicorn-8001-conflict.sh
set -e
DJANGO_DIR="/opt/1panel/apps/openresty/nginx/www/sites/43.142.166.152.443/django"
cd "$DJANGO_DIR"
echo "========== 修复前 =========="
pgrep -af 'gunicorn' || echo "无 gunicorn"
ss -ltnp 2>/dev/null | grep 8001 || true
echo ""
echo ">>> [1] 停 systemd 服务"
systemctl stop gunicorn-dianjing.service 2>/dev/null || true
sleep 2
echo ">>> [2] 结束所有 gunicorn含 1Panel 误起的)"
pkill -f 'gunicorn.*a_long_dianjing' 2>/dev/null || true
pkill -f 'gunicorn -c gunicorn.conf.py' 2>/dev/null || true
sleep 3
if ss -ltn 2>/dev/null | grep -q ':8001'; then
echo ">>> [3] 8001 仍占用,释放端口"
fuser -k 8001/tcp 2>/dev/null || true
sleep 2
fi
if ss -ltn 2>/dev/null | grep -q ':8001'; then
echo "错误: 8001 仍被占用,请检查是否有 1Panel Python 自动管理未关闭"
ss -ltnp | grep 8001 || true
exit 1
fi
echo ">>> [4] 只由 systemd 拉起唯一一套 gunicorn"
cp deploy/systemd/gunicorn-dianjing.service /etc/systemd/system/gunicorn-dianjing.service
systemctl daemon-reload
systemctl enable gunicorn-dianjing.service
systemctl start gunicorn-dianjing.service
sleep 3
echo ""
echo "========== 修复后 =========="
systemctl is-active gunicorn-dianjing.service
pgrep -af 'gunicorn' || echo "无 gunicorn"
NG=$(pgrep -cf 'gunicorn.*a_long_dianjing' 2>/dev/null || echo 0)
echo "进程数: $NG (正常=7: 1master+6worker)"
ss -ltnp 2>/dev/null | grep 8001 || true
curl -s -o /dev/null -w "curl 8001: %{time_total}s HTTP\n" http://127.0.0.1:8001/hqhd/
echo ""
echo ">>> 最近是否还有 8001 冲突:"
grep -a 'Connection in use' /tmp/gunicorn-error.log 2>/dev/null | tail -3 || echo "无"
echo ""
echo "【必做】1Panel → 该站点 → 关闭 Python 运行环境自动管理"
echo "否则 1Panel 会再次抢 8001全站又会卡死"

View File

@@ -8,12 +8,17 @@ cd "$DJANGO_DIR"
echo ">>> git pull"
git pull origin main
echo ">>> 更新 gunicorn systemd"
echo ">>> 更新 gunicorn systemd(先停再起,避免 8001 双开)"
cp deploy/systemd/gunicorn-dianjing.service /etc/systemd/system/gunicorn-dianjing.service
systemctl daemon-reload
systemctl enable gunicorn-dianjing.service
systemctl restart gunicorn-dianjing.service
systemctl stop gunicorn-dianjing.service 2>/dev/null || true
sleep 2
pkill -f 'gunicorn.*a_long_dianjing' 2>/dev/null || true
pkill -f 'gunicorn -c gunicorn.conf.py' 2>/dev/null || true
sleep 2
systemctl start gunicorn-dianjing.service
sleep 3
echo ">>> 状态"
systemctl is-active gunicorn-dianjing.service