(3.11.0) [root@xxxxxx ~]# ss -tulpn | grep :5000
tcp LISTEN 0 128 *:5000 *:* users:(("gunicorn",pid=111740,fd=5),("gunicorn",pid=111739,fd=5),("gunicorn",pid=111738,fd=5),("gunicorn",pid=111737,fd=5),("gunicorn",pid=111633,fd=5))
你现在查到 5000 端口是被多个
tcp LISTEN 0 128 *:5000 *:* users:(("gunicorn",pid=111740,fd=5),("gunicorn",pid=111739,fd=5),("gunicorn",pid=111738,fd=5),("gunicorn",pid=111737,fd=5),("gunicorn",pid=111633,fd=5))
你现在查到 5000 端口是被多个
gunicorn进程占用的(PID 分别是 111740、111739、111738、111737、111633),接下来我会告诉你如何安全、彻底地关闭这个端口,同时也会说明注意事项(避免误操作影响业务)。
第一步:终止占用 5000 端口的 gunicorn 进程
因为是多个 gunicorn 进程(属于同一个服务的子进程),有两种终止方式,推荐先尝试方式 1(优雅终止),无效再用方式 2(强制终止):方式 1:优雅终止(推荐,避免业务异常)
# 方式A:按主进程PID终止(优先找数值最小的PID,这里111633是主进程)
kill 111633
# 方式B:批量终止所有gunicorn进程(如果不确定主进程)
pkill gunicorn
方式 2:强制终止(优雅终止无效时用)
# 强制终止主进程
kill -9 111633
# 或批量强制终止所有gunicorn进程
pkill -9 gunicorn
第二步:验证 5000 端口是否已关闭
执行以下命令,无输出则说明端口已关闭:
ss -tulpn | grep :5000
关键注意事项(针对 gunicorn 服务)
- 防止进程重启:如果 gunicorn 是通过系统服务(systemd)自启动的,仅终止进程会导致服务自动重启,5000 端口会再次被占用。需先关闭自启动:
# 1. 先查找gunicorn对应的系统服务名(常见命名如gunicorn、app、web等) systemctl list-unit-files | grep -i gunicorn # 2. 关闭自启动(替换为实际服务名,如gunicorn) systemctl disable gunicorn # 3. 停止当前运行的服务(彻底终止进程) systemctl stop gunicorn - 权限问题:如果执行命令提示 “权限不足”,在所有命令前加
sudo(如sudo kill 111633)。 - 业务影响:gunicorn 通常是 Python Web 服务(如 Django/Flask)的运行容器,关闭端口会导致对应的 Web 服务无法访问,确认业务可停止后再操作。
总结
- 核心操作:通过
kill 主进程PID(111633)或pkill gunicorn终止占用 5000 端口的 gunicorn 进程,验证端口是否关闭。 - 关键避坑:若 gunicorn 是系统服务,需先执行
systemctl disable/stop 服务名防止进程重启。 - 操作优先级:优先优雅终止(kill),无效再强制终止(kill -9),避免数据异常。
声明:本文网友投稿,观点仅代表作者本人,不代表鲁班SEO赞同其观点或证实其描述。
