Search code examples
httpubuntutcpkillps

Kill already in use binding tcp connection


Kill already in use binding tcp connection

I ran a web application with the following log:

2015/01/05 12:57:56 Listening http://0.0.0.0:80
2015/01/05 12:57:56 listen tcp :80: bind: address already in use

It was not working because I already ran this before I ran the script.

Is there anyway that I can force to unbind or kill the process that binds this port so that I can rerun the script and serve the same port with different code?

I am using ubuntu and want to kill the binding tcp using ps or kill command.

Thanks!


Solution

    1. Use netstat to figure out which process is listening
    2. Kill the corresponding process

    Assuming root permission:

    kill $(sudo netstat -tlpn | 
           perl -ne 'my @a = split /[ \/]+/; print "$a[6]\n" if m/:80 /gio')