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

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 <em class="nl">process.on('uncaughtException', ..)</em>
  • And on kill do <em class="nl">process.on('SIGTERM', ..)</em>

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

Original: https://www.cnblogs.com/kungfupanda/p/16399041.html
Author: 功夫 熊猫
Title: How to kill server when seeing “EADDRINUSE: address already in use”

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/552375/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球