Ubuntu – Oneliner command to use kill given tcp port number instead of PID

bashcommand linekillscripts

I often do e.g.

sudo netstat -lpn |grep :8088

view the output

tcp6       0      0 :::8088                 :::*                    LISTEN      11189/java

and then

sudo kill -kill 11189

I'd like to have a more convenient command exactly like killatport 8088 that uses the tcp port number as a variable and that I can make as an alias to a pipeline that does what I want, but how do I get the PID from the output and pipe it to the kill command? I suppose I might be able to use awk to get the PID from the output from netstat, but how do I safeguard and make an exact port match so that the input 80 won't match 8080 and likewise? Should I make it a C program instead? Or is there already a small utility like this?

Best Answer

fuser can do that:

sudo fuser -KILL -k -n tcp 8088
Related Question