I'm using tomcat, and sometimes when I tell it to stop it doesn't properly kill the process.

My way around this is too do:

lsof -i tcp:8080

which outputs:

java    888 root   35u  IPv6 780659      0t0  TCP *:http-alt (LISTEN)
java    888 root   39r  IPv6 790103      0t0  TCP localhost:58916->localhost:http-alt (CLOSE_WAIT)
java    888 root   40r  IPv6 792585      0t0  TCP localhost:58936->localhost:http-alt (CLOSE_WAIT)
java    888 root   75r  IPv6 785553      0t0  TCP localhost:58701->localhost:http-alt (CLOSE_WAIT)
java    888 root   77r  IPv6 787642      0t0  TCP localhost:58814->localhost:http-alt (CLOSE_WAIT)
java    888 root  130u  IPv6 783894      0t0  TCP localhost:58686->localhost:http-alt (CLOSE_WAIT)
java    888 root  353u  IPv6 780929      0t0  TCP localhost:58632->localhost:http-alt (CLOSE_WAIT)

I then run

kill -9 pid

I want a way to get all the pid numbers and kill them. Thing is I don't know how to isolate that field.

  • 1
    If you're sure you only have one tomcat process open, you can use killall -9 tomcat – Joseph R. Sep 16 '13 at 21:49

There is a -t (terse) option in lsof, which seems to do exactly what you are looking for i.e.

$ sudo lsof -ti tcp:80

See man lsof

-t       specifies  that  lsof should produce terse output with process
         identifiers only and no header - e.g., so that the output  may
         be piped to kill(1).  -t selects the -w option.

Assuming you have the necessary permissions, you can pass the result to kill as a list of PIDs with command substitution:

kill -9 $(lsof -ti tcp:80)
  • You've now answered both of the questions I've asked btw ;-) – user2757729 Sep 17 '13 at 13:31

Do not forget the --no-run-if-empty option of kill :)

lsof -ti :8080 | xargs --no-run-if-empty kill -9

That way kill will only be run there is a process listening, not need to do the check yourself.

  • '--no-run-if-empty' is not supported in BSD(mac) – dinesh ygv May 7 at 9:05

lsof -i tcp:8080 produces the output, then | egrep -v "COMMAND PID USER" drops the header line, then | awk '{print $2}' prints the 2nd field, | sort -n prepares the numbers for | uniq, which only outputs each unique PID once. Putting it all together gives:

 lsof -i tcp:8080 | egrep -v "COMMAND PID USER" | awk '{print $2}' | sort -n | uniq  

But, pkill -KILL tomcat or killall -KILL tomcat is easier.

  • Tomcat process is not named "tomcat" that's the problem. It is just a normal java process, extra work has to be done to identify the correct process if there are other java processes running at the same time. – Terry Wang Sep 17 '13 at 0:18
  • @TerryWang Yup, that's the problem I had as well. Steeldrivers answer is working great. – user2757729 Sep 17 '13 at 13:31

The one liner from @waltinator is great.

I'll add some more flavor to it:

lsof -i tcp:8080 | egrep -v "COMMAND PID USER" | awk '{print $2}' | sort -n | uniq | xargs kill -9


kill -9 $(lsof -i tcp:8080 | egrep -v "COMMAND PID USER" | awk '{print $2}' | sort -n | uniq)

NOTE: this is still very basic, you may need to add more salt and pepper to make it more robust in a real environment.


This is the script I came up with a little bit of error checking.



if ! [[ "$PORT" =~ ^[0-9]+$ ]] ;
  printf "error: '$PORT' is not a number.\n\nUsage killport <port number>\n"
  exit 1

PID=$(lsof -ti:$PORT)

if ! [[ "$PID" =~ ^[0-9]+$ ]] ;
  printf "no proccess found, nothing to kill.\n"
  exit 0

printf "killing process $PID running on $PORT\n"
kill -9 $PID

Here is a simple fish shell function

function kill-port
  set pids (lsof -ti tcp:$argv)
  if test $pids
    kill -9 $pids
    echo "No proccesses on that port to kill to see for your self -- lsof -i tcp:$argv"

just stick this sucker in a file at this location ~/.config/fish/functions/kill-port.fish and your good to go. You can call it like kill-port 8000

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.