最佳方案 Error: listen EADDRINUSE: address already in use

这里的问题是,在第一次运行后不会正常退出节点进程。因此,节点仍绑定到该端口。使用 ps aux | grep node, 将显示这一点。使用 CTRL+C 退出应用程序,而不是使用 CTRL+Z 终止应用程序。这将正常退出应用程序,并删除端口绑定。
以正确的方式退出进程将在关闭时释放该端口。这将允许你重新启动该过程,而无需在能够重新运行它之前自行终止它的麻烦。

有一种方法是 killall -9 node 或 在控制台输入lsof -i:端口号,查看被占用进程的pid,再输入 kill -9 pid 即可杀死进程。这些方法按预期工作并解决问题,但你可能想要知道为什么 kill-9 可能不是最好的方法。

最重要的是,您可能希望以单个进程为目标,而不是盲目地终止所有活动进程。

在这种情况下,首先获取该端口上运行的进程的流程 ID (PID)(例如 3000):

lsof -i tcp:3000

这将返回类似内容:

COMMAND   PID   USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
node    64419 samyao   22u  IPv6 0x881da4f2a0d52ded      0t0  TCP *:3000 (LISTEN)

然后就做

kill -9 64419

ps - 实际上这是不好的办法。请继续阅读以下内容:

Generally, you should use kill (short for kill -s TERM, or on most systems kill -15) before kill -9 (kill -s KILL) to give the target
process a chance to clean up after itself. (Processes can’t catch or
ignore SIGKILL, but they can and often do catch SIGTERM.) If you don’t
give the process a chance to finish what it’s doing and clean up, it
may leave corrupted files (or other state) around that it won’t be
able to understand once restarted. strace/truss, ltrace and gdb are generally good ideas for looking at why a stuck process is stuck. (truss -u on Solaris is particularly helpful; I find ltrace too often presents arguments to library calls in an unusable format.) Solaris also has useful /proc-based tools, some of which have been ported to Linux. (pstack is often helpful).

通常,你应该在kill -9 之前使用 kill -15 来给目标进程一个自行清理的机会。(进程无法捕获或忽略 SIGKILL,但它们可以而且经常捕获 SIGTERM。如果不给进程一个机会来完成它在做什么和清理,它可能会留下损坏的文件(或其他状态)

因此,正如所述,你应该使用:

kill -15 64419
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值