Gunicorn Connection in Use: (‘0.0.0.0’, 8000)

I develop web services using python flask. One of the common error that I see while deploying the application is “Gunicorn Connection in Use: (‘0.0.0.0’, 8000)”.

This means that the port 8000 is busy with some other running process. But when I check the status of the port with the following command, I get empty response. That means there are no active application using the port. Some stale process is making the port busy.

netstat -tulpn | grep 8000

I even tried with the ps command to see any active process, but that also did not help.

ps -aux

If the ps command list the process, we can kill the process directly using the kill command

kill -9 {PID}

In my case I do not have the PID. So the only option to kill these kind of zombie application by using the below command.

sudo fuser -k {PORT}/tcp

In my case, the port number is 8000, so the command will be.

sudo fuser -k 8000/tcp

This trick helped me several times, hope this helps someone else also.

 

How to find and kill a process locking a particular port in Linux?

Sometimes because of some issue or bug, our application may stop working, but the port will be locked. This kind of issue is very common with MySQL server, Elasticsearch, WebServices, Tomcat etc. In such scenarios, we have to find the zombie process and kill it to free up the locked port.

How to find the process that locks the port?

Use the following command

netstat -tulpn | grep <port>

This output of this command will contain the process id. Now we just need to kill the process.

Verify the process

Before killing the process, figure out what process it is and ensure we are not killing any required processes.

ps -aux | grep <process id>

The output of the above command will give the details of the process.

How to Kill a process ?

After confirming the details, you can kill the process

kill -9 <process id>

Now verify whether the port is still locked or not by executing the netstat command again