Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
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
up vote 4 down vote accepted

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)
share|improve this answer
This is awesome. Nice and terse. Thanks! – user2757729 Sep 17 '13 at 13:30
You've now answered both of the questions I've asked btw ;-) – user2757729 Sep 17 '13 at 13:31

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.

share|improve this answer
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.

share|improve this answer

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
share|improve this answer

Your Answer


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.