bind: address already in use exit status 1端口占用

解决项目中的端口与进程占用问题:查找与终止策略
本文介绍了在项目中遇到端口或进程占用问题时,如何使用lsof、netstat和psaux等命令查找对应进程,以及kill和kill-SIGINT命令的不同用途,同时提及了pkill和killall等辅助工具。

项目场景:

在使用代码或执行操作时遇到端口或进程占用问题 ,导致无法启动,可通过下面命令方法解决


一 找到对应进程或端口号

`提示:这里提供三种查找方法

第一种

lsof -i :8080

此命令用于列出在指定端口上打开的所有网络连接及其相关的进程。它将显示指定端口上正在监听的进程以及已建立的连接。这个命令非常适合查找某个特定端口上正在运行的进程。

第二种

netstat -tuln | grep 8080

此命令组合用于列出当前正在监听的TCP/UDP端口,并筛选出包含指定端口的行。它主要用于检查特定端口是否已被占用,但不提供与进程相关的详细信息。

第三种

ps aux | grep main.go

此命令组合用于搜索并显示包含 “main.go” 的命令所对应的进程。它通过 ps aux 命令列出当前系统中所有进程的详细信息,然后通过 grep main.go 过滤出包含 “main.go” 的行。这个命令适用于查找正在运行特定命令的进程。

综上所述,三个命令的主要区别在于它们的用途和查询方式:

区别

lsof -i :8080用于查找特定端口上运行的进程和相关连接。
netstat -tuln | grep 8080用于检查特定端口是否已被占用
ps aux | grep main.go用于查找执行包含 “main.go” 的命令的进程


二 杀掉进程或端口号

第一种:干掉当前进程

 kill -SIGINT <进程ID>

第二种:给进程中断信号

kill <PID>

如不行可执行强杀命令:kill -9 <PID>

区别

kill <PID>:这个命令使用进程ID(PID)来终止进程。例如,kill 1234 将向PID为 1234 的进程发送默认的终止信号(SIGTERM),通常是终止进程的常用方式。

kill -SIGINT <进程ID>:这个命令使用指定的信号(其中 SIGINT 表示键盘输入的中断信号)来终止进程。例如,kill -SIGINT 1234 将向PID为 1234 的进程发送中断信号。

一般情况下,使用 kill 将会终止进程。而使用 kill -SIGINT <进程ID> 可以选择性地发送不同类型的信号给进程。

其他工具

pkill 或 killall 命令(用的不多,可自行查找)

上诉为个人总结,如有不足,欢迎补充,希望对大家有所帮助!

03-16
journalctl -xeu nginx.service ░░ Support: http://www.ubuntu.com/support ░░ ░░ A start job for unit nginx.service has finished with a failure. ░░ ░░ The job identifier is 1508154 and the job result is failed. Sep 17 15:38:30 zhyyyy systemd[1]: Starting nginx.service - nginx - high performance web server... ░░ Subject: A start job for unit nginx.service has begun execution ░░ Defined-By: systemd ░░ Support: http://www.ubuntu.com/support ░░ ░░ A start job for unit nginx.service has begun execution. ░░ ░░ The job identifier is 1508257. Sep 17 15:38:30 zhyyyy nginx[1949821]: nginx: [emerg] bind() to 0.0.0.0:1443 failed (98: Address already in use) Sep 17 15:38:30 zhyyyy nginx[1949821]: nginx: [emerg] bind() to 0.0.0.0:1443 failed (98: Address already in use) Sep 17 15:38:31 zhyyyy nginx[1949821]: nginx: [emerg] bind() to 0.0.0.0:1443 failed (98: Address already in use) Sep 17 15:38:31 zhyyyy nginx[1949821]: nginx: [emerg] bind() to 0.0.0.0:1443 failed (98: Address already in use) Sep 17 15:38:32 zhyyyy nginx[1949821]: nginx: [emerg] bind() to 0.0.0.0:1443 failed (98: Address already in use) Sep 17 15:38:32 zhyyyy nginx[1949821]: nginx: [emerg] still could not bind() Sep 17 15:38:32 zhyyyy systemd[1]: nginx.service: Control process exited, code=exited, status=1/FAILURE ░░ Subject: Unit process exited ░░ Defined-By: systemd ░░ Support: http://www.ubuntu.com/support ░░ ░░ An ExecStart= process belonging to unit nginx.service has exited. ░░ ░░ The process' exit code is 'exited' and its exit status is 1. Sep 17 15:38:32 zhyyyy systemd[1]: nginx.service: Failed with result 'exit-code'. ░░ Subject: Unit failed ░░ Defined-By: systemd ░░ Support: http://www.ubuntu.com/support ░░ ░░ The unit nginx.service has entered the 'failed' state with result 'exit-code'. Sep 17 15:38:32 zhyyyy systemd[1]: Failed to start nginx.service - nginx - high performance web server. ░░ Subject: A start job for unit nginx.service has failed ░░ Defined-By: systemd ░░ Support: http://www.ubuntu.com/support ░░ ░░ A start job for unit nginx.service has finished with a failure. ░░ ░░ The job identifier is 1508257 and the job result is failed.
09-18
[root@localhost ~]# systemctl restart nginx Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details. [root@localhost ~]# systemctl is-active nginx failed [root@localhost ~]# start nginx -bash: start: 未找到命令 [root@localhost ~]# systemctl start nginx Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details. [root@localhost ~]# ^C [root@localhost ~]# systemctl status nginx.service ● nginx.service - nginx - high performance web server Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled) Active: failed (Result: exit-code) since 四 2025-09-18 09:22:50 CST; 11s ago Docs: http://nginx.org/en/docs/ Process: 14473 ExecStop=/bin/sh -c /bin/kill -s TERM $(/bin/cat /var/run/nginx.pid) (code=exited, status=1/FAILURE) Process: 14260 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=1/FAILURE) Main PID: 18097 (code=exited, status=0/SUCCESS) 9月 18 09:22:48 localhost.localdomain nginx[14260]: nginx: [emerg] bind() to 0.0.0.0:9999 failed (98: Address already in use) 9月 18 09:22:49 localhost.localdomain nginx[14260]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) 9月 18 09:22:49 localhost.localdomain nginx[14260]: nginx: [emerg] bind() to 0.0.0.0:9999 failed (98: Address already in use) 9月 18 09:22:49 localhost.localdomain nginx[14260]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) 9月 18 09:22:49 localhost.localdomain nginx[14260]: nginx: [emerg] bind() to 0.0.0.0:9999 failed (98: Address already in use) 9月 18 09:22:50 localhost.localdomain nginx[14260]: nginx: [emerg] still could not bind() 9月 18 09:22:50 localhost.localdomain systemd[1]: nginx.service: control process exited, code=exited status=1 9月 18 09:22:50 localhost.localdomain systemd[1]: Failed to start nginx - high performance web server. 9月 18 09:22:50 localhost.localdomain systemd[1]: Unit nginx.service entered failed state. 9月 18 09:22:50 localhost.localdomain systemd[1]: nginx.service failed.
09-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值