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?

  • 1
    Using SIGKILL is usually a bad idea. Any reason why you don't want the process to clean up after itself? – geirha Feb 21 '14 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. – Niklas Rosencrantz Feb 21 '14 at 19:03
  • 1
    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 '14 at 21:50

fuser can do that:

sudo fuser -KILL -k -n tcp 8088

A command can be formulated like this:

netstat -lpn | grep ":1234\b" | awk '{sub(/\/.*/, "", $NF); print $NF}' | xargs -i kill -kill {}


  1. netstat -ltpn

    • This lists the listening ports (l) on TCP (t) and their programs (p) without resolving port numbers to names (n).
  2. grep ":1234\b"

    • This searches for :1234 followed by a boundary (\b), indicating the end of the word (or number, in our case). This makes sure that we don't catch :12345 for example.
  3. awk '{sub(/\/.*/, "", $NF); print $NF}'

    • This

      • substitutes sub(/regex/,"replacewith", #fieldnumber)
      • this regex \/.*
      • with nothing ""
      • in the field $NF, which means the last field (i.e. the field that contains PID/program)
      • then prints it print $NF.

      The regex \/.* matches a literal / and everything after it, and then we're replacing it with nothing, essentially deleting it, so we're left with only the PID number in that field.

  4. xargs -i kill -kill {}

    • xargs -i is a program that allows you to make the output of the previous command act as the input to another command. Our command is kill -kill {} where {} indicates "the output from the previous command in the pipeline", which is our PID number.

Note: this whole command might be a little bit dangerous because you might accidentally kill something you didn't want to. It could use with a little bit more sanitization. Just make sure you get the correct port number when using it.

If you want to make this into a function, you can add the following to your ~/.bashrc:

    netstat -lpn | grep ":$1\b" | awk '{sub(/\/.*/, "", $NF); print $NF}' | xargs -i kill -kill {}

Save and apply the changes using source ~/.bashrc. Now, you can use the function like this:

killatport 8088

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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