loading...

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

dechamp profile image DeChamp Updated on ・2 min read

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)

-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!

Posted on by:

dechamp profile

DeChamp

@dechamp

Just a coder and a dad. I love my family and I love to code!!!! started coding at 11, so I have 25 years under my belt. Still love learning about it every day. Follow me on instagram @codeFiend

Discussion

pic
Editor guide
 

How about "different, not better"?

fuser -kn tcp $PORT_NUMBER will try to drop a kill -9 on any process using the specified TCP port.

Out of deference to Ben Sinclair's thoughtful counsel you can also specify a signal like this: fuser -$SIGNAL_NUMBER_OR_NAME -kn tcp $PORT_NUMBER

 

Tried playing around with this and read up on the man page. Seems like it doesn't know about options -k or -n (-kn). I tried variations and none worked.

 

How does that old joke about IT standards go, again? : \

The GNU fuser implementation from psmisc accepts -n and -k options, but distribution is far from universal.

 

I have a better solution!

It's kill without the -9. I think that when people give that as a solution, it's like when people put sudo in front of every command in their examples, or when people recommend something like chmod -R 777 as a solution to a permissions problem.

I mean, it's probably ok, but SIGKILL is the equivalent of wrenching your USB stick out while it's in use or turning your PC off before Windows 95 says it's safe to shut down.

 

This is very true. I do agree with you, as to why I mentioned it's risky. The only thing I've noticed is that pretty much every time I go to kill something running on my needed port, it just won't die! ha ha. So yes, start without the -9 for sure!