Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question
    
Using SIGKILL is usually a bad idea. Any reason why you don't want the process to clean up after itself? –  geirha Feb 21 at 13:59
    
Stopping the server as mvn jetty:stop could fail if the instance has OutOfMemoryError. When I restart java servlets it happens that the port is not available even at a regular stop such as mvn jetty:stop. Sometimes the process can get OutOfMemoryError and won't free the TCP port at a regular shutdown such as mvn jetty:stop. –  909 Niklas Feb 21 at 19:03
    
Still, mvn jetty:stop is not the same as sending SIGTERM, and the jvm should still be able to process SIGTERM even though its application(s) is out of mem. –  geirha Feb 21 at 21:50
add comment

1 Answer

up vote 6 down vote accepted

fuser can do that:

sudo fuser -KILL -k -n tcp 8088
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.