the dreaded port error "bind: address already in use", kill it!

DeChamp - Sep 18 '19 - - Dev Community

tl;dr: lsof -i :<PORT>, then get PID and run next command with it (be careful running this command) kill <PID>. If you want a all in one, you can use this! kill $(lsof -i :<PORT> | awk 'NR>1 {print $2}')

So you're cruising along and you go to start your service, just to find out that you had something running on that same port you needed.

DOH!

Well here is a quick solution. Just swap out with lets say 3000, or what ever your port is.

lsof -i :<port>

You should see something like this. If you get a empty response, then nothing is running on the port.
output of lsof command

Now you had the PID, which for our example is 28913.

This next part needs your attention! I'm going to show you how to kill the process but ONLY DO THIS IF YOU TRUST YOU KNOW THE PROCESS you're about to kill.

If you kill a process that you have no clue what is doing, it could affect your system in a negative way.

If you kill a app that is in the middle of saving, it could corrupt your data.

So just be smart before blindly running the next command.

I see that it's com.docke so I know it's docker. I'm find killing it.

kill -9 28913.

UPDATE
So as Ben Sinclair mentioned, avoid using -9 with kill since it's dangerous. First try kill $PORTNUMBER

You now freed up the port. If you run man kill you'll see a list of the different signals.

 1       HUP (hang up)
 2       INT (interrupt)
 3       QUIT (quit)
 6       ABRT (abort)
 9       KILL (non-catchable, non-ignorable kill)
 14      ALRM (alarm clock)
 15      TERM (software termination signal)
Enter fullscreen mode Exit fullscreen mode

-9 forces it to quit no matter what.

If you want to do a one in all, where it looks up the port and then tries to kill it right away, you can do this command. kill $(lsof -i :<PORT> | awk 'NR>1 {print $2}'), if you get the message kill: not enough arguments then it's because no process is found for the port so your port should be free. Otherwise, it'll kill and return a new empty line on success.

Have a better solution? Leave a comment!


Varymade LLC.

Current projects are https://charactergenerator4000.com and https://coder.exchange. Please check them out and let us know your thoughts.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player