Port XXXX already in use: How to solve it?

Port XXXX already in use: How to solve it?

Photo by Dim Hou on Unsplash

Hi everyone,

While developing the application, you come across the problem with Port XXXX already in use. Your system has already run the application that uses your port in the background. And If you rerun your application, you get an error like Port XXXX already in use.

In this situation, you can get out of it fairly quickly. The solution would be the following.

  • Find the running port process identification number(PID)
  • Kill the process identification number(PID)

The above path will solve the problem you occurred easily.

I will try to show you how to find a process identification number (PID) on UNIX and how to kill them all. There are more than one ways to do that. I will use lsof and kill commands to solve this problem.

lsof is a list of files. The command allows you to get a list of files that have been opened. Basically, it gives you information on which files have been opened by which process.

You can find more information with man lsof command on your terminal.

NAME
       lsof - list open files

SYNOPSIS
       lsof [ -?abChlnNOPRtUvVX ] [ -A A ] [ -c c ] [ +c c ] [ +|-d d ] [ +|-D D ] [ +|-e s ] [ +|-E ] [ +|-f [cfgGn] ] [ -F [f] ] [ -g
       [s] ] [ -i [i] ] [ -k k ] [ -K k ] [ +|-L [l] ] [ +|-m m ] [ +|-M ] [ -o [o] ] [ -p s ] [ +|-r [t[m<fmt>]] ] [ -s [p:s] ] [ -S
       [t] ] [ -T [t] ] [ -u s ] [ +|-w ] [ -x [fl] ] [ +|-X ] [ -z [z] ] [ -Z [Z] ] [ -- ] [names]

DESCRIPTION
       Lsof revision 4.91 lists on its standard output file information about files opened by processes for the following UNIX
       dialects:

            Apple Darwin 9 and Mac OS X 10.[567]
            FreeBSD 8.[234], 9.0 and 1[012].0 for AMD64-based systems
            Linux 2.1.72 and above for x86-based systems
            Solaris 9, 10 and 11

       (See the DISTRIBUTION section of this manual page for information on how to obtain the latest lsof revision.)

       An open file may be a regular file, a directory, a block special file, a character special file, an executing text reference, a
       library, a stream or a network file (Internet socket, NFS file or UNIX domain socket.)  A specific file or all the files in a
       file system may be selected by path.

       Instead of a formatted display, lsof will produce output that can be parsed by other programs.  See the -F, option description,
       and the OUTPUT FOR OTHER PROGRAMS section for more information.

       In addition to producing a single output list, lsof will run in repeat mode.  In repeat mode it will produce output, delay, then
       repeat the output operation until stopped with an interrupt or quit signal.  See the +|-r [t[m<fmt>]] option description for
       more information.

Finding port information with lsof command in UNIX is easy.

I will use the 8080 port for my example. I have run the Spring Boot application in the 8080 port and I will kill the process in the terminal with the following steps.

lsof -i:8080 command will show you the whole information about the port you need.

lsof -t -i:8080command will give you just the PID number in this port.

  • -t - show only process ID
  • -i - show only internet connections
  • :8080 - show only processes for given port.
$ lsof -i:8080
COMMAND   PID         USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
java    60586 erkanguzeler   86u  IPv6 0x20f3b74184a9fc11      0t0  TCP *:http-alt (LISTEN)

$ lsof -t -i:8080
60586

After finding the port, the last section is easy for us. We just need to kill the port PID number will kill command.

sudo kill -9 <PID>
  • kill : kill the process command
  • -9 : force to kill
# erkanguzeler
$ sudo kill -9 60586
# erkanguzeler
$ lsof -i:8080
// nothing to show

If you want to one-line command to kill the PID process for your port. You can also use the below command in your terminal.

sudo kill -9 $(sudo lsof -t -i:<PORT>)

Conclusion

You can kill an application port already in use in your system by using this post. I believe that there are many easy ways to solve this kind of problem. This is one of my ways of resolving it. I hope this helps you when developing an application.

I hope you enjoy reading.

Have a nice coding.