Kill already running ports with ease
· 1 mins read · Workflow
One of the most annoying things during development is the moment we face the following error in command line while trying to launch an application locally:
Server Error: this port is already in use
Most of the times this means that we have forgotten a process running in the background or that we thought that we killed a process but we actually didn't, since we didn't use
CTRL + C to kill it the right way etc
This is probably the moment that most developers start searching around the web to find a solution so the marathon for copy-n-pasting random commands in terminal begins 🙈
# A common approach
On a Linux / Mac machine, this might save the day for you:
lsof -ti tcp:[PORT] | xargs kill
Let's break this into steps to understand some more what is going on here:
lsofis used to
List Open Filesso we can spot processes that listen on a specific port
-iis used to search for the wanted port
-tis used to return only the
PID(process id) we are looking for
|is used to pass the list of
PIDwe found, to the next command
xargsis used to apply
killto each of these
killis actually killing each
PID. We might need to use
kill -9if processes need to be killed by force
But, who can memorize all these, right? 😬
# An easier approach
Let's see an even more straightforward solution we can use:
# One port npx kill-port [PORT] # Multiple ports npx kill-port [PORT1] [PORT2] [PORTN]
Easier, right? By doing so, we pull the npm package
kill-port so it can do the dirty work for us.
It is far more declarative and self-explanatory so it is definitely easier to remember.
Get notified about latest posts and updates once a week!!
You liked it?