ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

How to kill server when seeing “EADDRINUSE: address already in use”

2022-06-22 02:32:07  阅读:234  来源: 互联网

标签:EADDRINUSE seeing use already process PID kill


How to kill server when seeing “EADDRINUSE: address already in use”

 

https://stackoverflow.com/questions/4075287/node-express-eaddrinuse-address-already-in-use-kill-server

https://levelup.gitconnected.com/how-to-kill-server-when-seeing-eaddrinuse-address-already-in-use-16c4c4d7fe5d

================================

A tutorial on how to kill the process manually when “EADDRINUSE” happens on Mac/Linux and Windows.

Nodejs listen EADDRINUSE: address already in use

The Problem

When trying to restart a Node application, the previous one did not shut down properly, and you may see a “listen EADDRINUSE: address already in use” error such as:

listen EADDRINUSE: address already in use

The cause behind this issue

The reason behind this is that

process.on('exit', ...) isn't called when the process crashes or is killed. It is only called when the event loop ends, and since server.close() sort of ends the event loop (it still has to wait for currently running stacks here and there) it makes no sense to put that inside the exit event.

Solution

The proper fix for the application would be

  • On crash, do process.on('uncaughtException', ..)
  • And on kill do process.on('SIGTERM', ..)

When this EADDRINUSE issue has already happened, in order to resolve it, you need to kill the process manually. In order to do that, you need to find the process id (PID) of the process. You know the process is occupying a particular port on your machine or server.

Kill the process manually

For Mac/Linux

To find the process id (PID) associated with the port

⇒ lsof -i tcp:3000 
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 44475 chen5 31u IPv4 0x8b1721168764e4bf 0t0 TCP *:strexec-s (LISTEN)

Then to kill the process

kill -9 44475

Use -9 option to make sure the process dies immediately

If you get permissions errors, you may need to use the sudo keyword, for example:

sudo kill -9 44475
=============================

First, you would want to know which process is using port 3000

sudo lsof -i :3000

this will list all PID listening on this port, once you have the PID you can terminate it with the following:

kill -9 {PID}


=============================

For Windows

Solution 1: Task Manager

Open the Task Manager application (taskman.exe), from either the Processes or Services tab sort by the PID column. To display the PID column, right-click the header row and select PID from the list. Right-click the process you want to stop and select End task.

example in Windows, source from Internet

Solution 2: Use Command prompt

Open a CMD window in Administrator mode by navigating to Start > Run > type cmd > right-click Command Prompt, then select Run as administrator.

source from Google Search

Use the netstat command lists all the active ports. The -a switch displays all ports in use, not just the ports associated with the current user. The -n option stops a hostname lookup (which takes a long time). The -o option lists the process ID that is responsible for the port activity. The findstr command matches the header row that contains the PID string, and the port you are looking for, in a port format with the preceding colon, is :3000.

C:\Users\admin>netstat -ano|findstr "PID :3000" 
Proto Local Address Foreign Address State PID
TCP 0.0.0.0:3000 0.0.0.0:0 LISTENING 18264

To kill this process (the /f is force):

taskkill /pid 18264 /f

标签:EADDRINUSE,seeing,use,already,process,PID,kill
来源: https://www.cnblogs.com/kungfupanda/p/16399041.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有