You’ve started a development server on your favorite :8080
localhost port and closed your IDE. But the server is still running in the background and if you try to start a new one, you get a message similar to this:
ERROR: listen tcp :8080: bind: address already in use
Yeah, me too. Let’s track it down and kill it for good.
List process listening on a port
lsof -i:8080
For me, this yields this result:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
main 65536 vojtechstruhar 3u IPv6 0xd9effee8308abdb 0t0 TCP *:http-alt (LISTEN)
See the PID
column? That what we’re after. Copy this process id number and terminate it:
kill 65536
If this doesn’t help, you can add the -9
flag, so that the process is forcibly terminated.
Shorthand
You can use the -t
flag for the lsof
command, so that only the PID of the process is printed. We can use this in a beautiful one-liner:
kill -9 $(lsof -t -i:8080)
If you find yourself doing this often, I’d say create a helper function in your .bashrc
or similar:
function portkill {
if [ "$#" -lt 1 ]; then
echo "Supply a port to kill as a first argument."
return 1
fi
echo "Killing process on port $1"
kill -9 $(lsof -t -i:$1)
}
Now that this function is available to you in your shell by default, just call portkill 8080
and you are good to go :)