Port is already in use - for any port on Mac - node.js

So recently I got a Macbook and I have cloned my project on it which is a MERN stack application. The port in my .env is 5000. When I attempt to start up the server I get the error that the port is already in use.
I figured "hmm ok, i'll change it to 5100". That worked, but only for that time, the next time I tried to run it i got the same error for port 5100.
Anyone have any idea what's going on?

Check the process Id
lsof -nP -iTCP -sTCP:LISTEN | grep 5000
Check the process name
ps -Ao user,pid,command | grep -v grep | grep <<pid>>
which will output something similar to this, which means your mac OS is utilising port 5000
622 /System/Library/CoreServices/ControlCenter.app/Contents/MacOS/ControlCenter
This is because new AirPlay functionality in mac Monterey version is utilising port 5000. To disable that you can
go to system preferences -> search for sharing -> untick AirPlay Receiver in left side panel

You could use
netstat -anvp tcp | awk 'NR<3 || /LISTEN/'
(credits: https://apple.stackexchange.com/a/117648)
and use
ps aux PID
to see the correspoding proccess. (PID is number from PID columen, ofc.)

This is due to the new AirPlay functionality on Monterrey Os.
Control Center stops listening to those ports when you turn off “AirPlay Receiver”.
In the “Sharing” System Preference, so uncheck airplay receiver:
After that you must kill the process that take the 500 port
lsof -nP -iTCP -sTCP:LISTEN | grep 5000
That show you which process to kill
Then, you can use the port 5000 again.

Related

Why HTTP ports stay open when using them by Nodejs servers?

I have a problem when launching a Nodejs script that listens in one of the HTTP ports. Sometimes, even if I stop the script, the used HTTP port stays "in use", making it impossible to use it another time. Today, i've set up NGINX in my linux and all the HTTP ports were "in use". I was obliged to restart my computer to solve the problem.
I wanted to know why is this happening ? What can i do to prevent it ? and in case an HTTP port stays "in use", how can i close it to be able to use again ?
Thanks for your help.
This is applicable only on Linux and MacOS, you can list all your used ports like that:
sudo lsof -i -P -n | grep LISTEN
Read more here about how to check if a port is in use: https://www.cyberciti.biz/faq/unix-linux-check-if-port-is-in-use-command/
You can also list the node processes:
top | grep node
or
ps -ef | grep node
Then you can kill the node processes like that:
killall node
Make sure that when you want to stop the server you are pressing CTRL + C

Error: listen EADDRINUSE :::9000, killall && kill -9 are not working [duplicate]

After starting an HTTP server process a couple of times, I get this error like an instance of Go has not stopped!?
listen tcp :9000: bind: address already in use
I have experienced something like this with Node.js too, but I was able to kill the process.. Unfortunately it seems like I can't find the process id and kill it..
How can I "free" the TCP port?
If you are on Unix-like system, you can use netstat to find out which process is listening on a port:
sudo netstat -nlp | grep 9000
It turns out the -p option is not available on OS X. If you are using OS X, you can do this:
lsof -n -i4TCP:$PORT | grep LISTEN
Who is listening on a given TCP port on Mac OS X?

What are the differences between lsof and netstat on linux?

I encounted a problem today:
When I started HDP docker container, an error occured:
listen tcp 0.0.0.0:8086: bind: address already in use
According to error message, I know that port 8086 was already in use, so I tried some commands to determine which program was using port 8086.
lsof -i:8086
lsof -i tcp:8086
lsof | grep 8086
But all of commands above make no outputs!
I felt really confused about that, after some searching on google, I tried another command:
netstat -pna | grep 8086
I got correct output from this command.
I know some differences between lsof and netstat, but I really do not know why I cannot get any output from lsof -i:8086?.
Here are some differences between two commands I searched from google:
netstat(net statistic) is connection based,it shows NW connections (udp/tcp ports), routing tables, interface, multi-cast membership, etc.
lsof(list of open files) is application based, this is kind of like netstat + ps, there you can see all accessed ports, NW connections, etc.
but lsof includes stuff like my local emacs window terminal session (tty dev/pts/n) which is not part of netstat
I faced a similar issue today. The solution was to run the lsof command with sudo privileges.
sudo lsof -i:8086
should print the desired output.
LSOF: List of Open Files. It lists all the open files belonging to all active processes.
Examples:
sudo lsof -n -i
sudo lsof -n -i4
sudo lsof -n -i :80
-n inhibits the conversion of network numbers to host names for network files. Inhibiting conversion may make lsof run faster. It is also useful when host
lookup is not working properly
-i selects the listing of files any of whose Internet address matches the address specified in i. If no address is specified, this option selects the listing of all Internet and x.25 (HP-UX) network files. If -i4 or -i6 is specified with no following address, only files of the indicated IP version, IPv4 or IPv6, are displayed.
NETSTAT: It is a tool to get the network statistics. By default, netstat displays a list of open sockets. If you don't specify any
address families, then the active sockets of all configured address
families will be printed.
Displays the kernel routing tables:
netstat -r
Display all listening and established connection for both TCP and UDP with PID data:
netstat -plunt
Additionally, You have another command line tool to use which is SS.
SS: It is used to dump socket statistics. It allows showing information similar to netstat. It can display more TCP and state
information than other tools.
-plunt gives data for the TCP and UDP connections which are established and listening with process information:
sudo ss -plunt
You should be root to get proper answers to your lsof questions. Your command is fine, assuming something really is listening on that port.
As you already mentioned, lsof is a very useful command which is used to list files opened by a specific process, while netstat is a tool for monitoring network connections.
You should be able to find the PID of the process listening on port 8086 with netstat:
netstat -tunlp |grep :8086
and then use lsof to list the files used by the process:
lsof -p PID

How to find out which port number a process is using and the process using a specific port number

I'm beginner with openhab, after launching the server /etc/init.d/openhab2 start i got that [ ok ] Starting openhab2 (via systemctl): openhab2.service, but I still have no access to the platform via http://localhost:8080/
I want to know :
which process is using port 8080
which port openhab runs on
thanks
you can use lsof,
lsof | grep TCP | grep 8080 and lsof | grep openhab | grep TCP
if a process forks and the child process is the one that is using a port it may not work as expected

How can I find a process using a TCP port?

After starting an HTTP server process a couple of times, I get this error like an instance of Go has not stopped!?
listen tcp :9000: bind: address already in use
I have experienced something like this with Node.js too, but I was able to kill the process.. Unfortunately it seems like I can't find the process id and kill it..
How can I "free" the TCP port?
If you are on Unix-like system, you can use netstat to find out which process is listening on a port:
sudo netstat -nlp | grep 9000
It turns out the -p option is not available on OS X. If you are using OS X, you can do this:
lsof -n -i4TCP:$PORT | grep LISTEN
Who is listening on a given TCP port on Mac OS X?

Resources