Different ways to kill or process or port or name in MacOS
When you start running rails, java, node, or any applications in MACOS, a process is created.
Sometimes, You may need to stop a single process or all processes for debugging.
Each service or operation in a MACOS creates a process.
Each process contains an Id and name. It throws an error if the same services started again while running. You see below the kind of errors
Rails already running Address already in use - bind(2) (Errno::EADDRINUSE)
You must first kill the process to stop it.
This tutorial explains how to Find and kill the process locking port on Mac.
How to kill port number and stop the process in macOS
There are many ways we can do this.
List out multiple ways in case one of the approaches is not working in Mac version types.
First approach, Find process by port name
netstat -vanp tcp | grep 3000
or find the process by name
ps -aef | grep rails
It gives a list of the process with id or name
You can kill using the below command
kill -9 processid
Second approach, You can use the port number directly for a single process
kill -9 $(lsof -ti:3000)
for multiple ports
kill -9 $(lsof -ti:3000,3200)
third approach using the npx node command, This works on the Node Environment
npx kill-port 3000 3200