others-how to solve 'Web server failed to start. Port 8080 was already in use.' exception or error when running web applications ?

1. Purpose

In this post, I would demo how to solve ‘Web server failed to start. Port xxxx was already in use.’ error when we run web applications.

2. Environment

  • Linux or MacOS

3. The problem

When we run a web service or web server , we got this:

***************************
APPLICATION FAILED TO START
***************************

Description:

Web server failed to start. Port 8080 was already in use.

Action:

Identify and stop the process that's listening on port 8080 or configure this application to listen on another port.

4. The solution

4.1 Find the PID of the web service or web server

To solve the problem,first, we need to find the PID of the process:

lsof -i:8080

What is PID?

In Linux and Unix-like systems, each process is assigned a process ID, or PID. This is how the operating system identifies and keeps track of processes

What is lsof?

lsof meaning ‘LiSt Open Files’ is used to find out which files are open by which process. As we all know Linux/Unix considers everything as a files (pipes, sockets, directories, devices etc).

To find out all the running process of specific port, just use the following command with option -i.

After running the above command, we got this result:

➜  bswen-springboot23 git:(main) ✗ lsof -i:8080
COMMAND    PID       USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
___1go_bu 4040 bswen    3u  IPv6 0x444a1d7b78fa8349      0t0  TCP *:http-alt (LISTEN)

According to the result, we found that the PID of the web service that occupied the port 8080 is 4040.

4.2 Check which process is occupying the port

Now we have found the PID of the process, we want to know details of the process, run this command:

➜  bswen-springboot23 git:(main) ✗ ps -ef|grep 4040 
  501  7456  7234   0 11:03AM ttys000    0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn 4040
  501  4040 79820   0  5:36PM ttys004    0:00.04 /private/var/folders/l1/bxn8rt2j6hzg6zc6gjxy_3rr0000gn/T/___1go_build_main_go__1_

We found that a Golang process is occupying the port 8080, so , when we start the new service, the error happens.

4.3 Kill the process that is occupying the port

Now we can kill the process as follows:

kill -9 4040

Now run the application again, it works!

5. Summary

In this post, we demonstrated how to solve the port-is-already-in-use error, the key point is find the PID of the web service that utilized the same port as the new web service.