How to find and kill a port
This is a simple approach which we need on daily basis which is find and kill a port.
Following are the steps to find and kill the port.
- You should find the process id which is known as pid using the port as follows.
sudo lsof -i :<port_id>
Following is a sample output for
sudo lsof -i :3000
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
docker-pr 299618 root 4u IPv4 3950094 0t0 TCP *:3000 (LISTEN)
2. Then get the pid which is bolded in the above example and kill it as follows.
kill [option] <pid>
kill -15 299618 // -15 for give time to safely cleanup and die.
or you can simply kill it without give time to cleanup.
kill -9 299618
Okay what if I don’t know what is the port id? Then list all the ports and find it as follows.
sudo lsof -nP -iTCP -sTCP:LISTEN
Important: If you don’t know the port but you know process name or anything you can simply use grep using pipe and filter out as follow.
sudo lsof -nP -iTCP -sTCP:LISTEN | grep mysqld
Above will filter processes with mysqld as follows.
Hope this will be helpful.
If you have found this helpful please hit that 👏 and share it on social media :).