Files
Django/deploy/fix-gunicorn-8001-conflict.sh

59 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
# 修复 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全站又会卡死"