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

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目场景:

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


一 找到对应进程或端口号

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

第一种

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
root@iZf8z2slk85lm15so7cuorZ:~# nginx -s reload 2025/03/10 14:49:22 [notice] 201951#201951: signal process started 2025/03/10 14:49:22 [error] 201951#201951: open() "/run/nginx.pid" failed (2: No such file or directory) root@iZf8z2slk85lm15so7cuorZ:~# systemctl status nginx.service × nginx.service - A high performance web server and a reverse proxy server Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; preset: enabled) Active: failed (Result: exit-code) since Mon 2025-03-10 14:48:30 CST; 1min 21s ago Docs: man:nginx(8) Process: 201838 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Process: 201839 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=1/FAILURE) CPU: 20ms Mar 10 14:48:28 iZf8z2slk85lm15so7cuorZ nginx[201839]: nginx: [emerg] bind() to 0.0.0.0:88 failed (98: Address already in use) Mar 10 14:48:28 iZf8z2slk85lm15so7cuorZ nginx[201839]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) Mar 10 14:48:29 iZf8z2slk85lm15so7cuorZ nginx[201839]: nginx: [emerg] bind() to 0.0.0.0:88 failed (98: Address already in use) Mar 10 14:48:29 iZf8z2slk85lm15so7cuorZ nginx[201839]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) Mar 10 14:48:29 iZf8z2slk85lm15so7cuorZ nginx[201839]: nginx: [emerg] bind() to 0.0.0.0:88 failed (98: Address already in use) Mar 10 14:48:29 iZf8z2slk85lm15so7cuorZ nginx[201839]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) Mar 10 14:48:30 iZf8z2slk85lm15so7cuorZ nginx[201839]: nginx: [emerg] still could not bind() Mar 10 14:48:30 iZf8z2slk85lm15so7cuorZ systemd[1]: nginx.service: Control process exited, code=exited, status=1/FAILURE Mar 10 14:48:30 iZf8z2slk85lm15so7cuorZ systemd[1]: nginx.service: Failed with result 'exit-code'. Mar 10 14:48:30 iZf8z2slk85lm15so7cuorZ systemd[1]: Failed to start nginx.service - A high performance web server and
03-13
[root@iZbp11irq8z6b1plo30y63Z ~]# systemctl status nginx.service --no-pager ● nginx.service - LSB: starts the nginx web server Loaded: loaded (/etc/rc.d/init.d/nginx; bad; vendor preset: disabled) Active: failed (Result: exit-code) since Mon 2025-03-10 09:10:25 CST; 1min 35s ago Docs: man:systemd-sysv-generator(8) Process: 32724 ExecStop=/etc/rc.d/init.d/nginx stop (code=exited, status=1/FAILURE) Process: 6383 ExecStart=/etc/rc.d/init.d/nginx start (code=exited, status=1/FAILURE) Mar 10 09:10:23 iZbp11irq8z6b1plo30y63Z nginx[6383]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) Mar 10 09:10:24 iZbp11irq8z6b1plo30y63Z nginx[6383]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) Mar 10 09:10:24 iZbp11irq8z6b1plo30y63Z nginx[6383]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) Mar 10 09:10:25 iZbp11irq8z6b1plo30y63Z nginx[6383]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) Mar 10 09:10:25 iZbp11irq8z6b1plo30y63Z nginx[6383]: nginx: [emerg] still could not bind() Mar 10 09:10:25 iZbp11irq8z6b1plo30y63Z nginx[6383]: failed Mar 10 09:10:25 iZbp11irq8z6b1plo30y63Z systemd[1]: nginx.service: control process exited, code=exited status=1 Mar 10 09:10:25 iZbp11irq8z6b1plo30y63Z systemd[1]: Failed to start LSB: starts the nginx web server. Mar 10 09:10:25 iZbp11irq8z6b1plo30y63Z systemd[1]: Unit nginx.service entered failed state. Mar 10 09:10:25 iZbp11irq8z6b1plo30y63Z systemd[1]: nginx.service failed. [root@iZbp11irq8z6b1plo30y63Z ~]#
03-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值