33

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:

COMMAND PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
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
  • 1
    If you're sure you only have one tomcat process open, you can use killall -9 tomcat
    – Joseph R.
    Sep 16, 2013 at 21:49

6 Answers 6

68

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
1387
4538
4539

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)
1
  • You've now answered both of the questions I've asked btw ;-) Sep 17, 2013 at 13:31
5

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.

2
  • '--no-run-if-empty' is not supported in BSD(mac)
    – dinesh ygv
    May 7, 2019 at 9:05
  • --no-run-if-empty worked for me on Mac, but I needed to call lsof -i rather than -ti. Apr 30, 2020 at 10:40
1

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.

2
  • 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, 2013 at 0:18
  • @TerryWang Yup, that's the problem I had as well. Steeldrivers answer is working great. Sep 17, 2013 at 13:31
0

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

OR

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.

0

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

#!/bin/bash

PORT=$1

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

PID=$(lsof -ti:$PORT)

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

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

Here is a simple fish shell function

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

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .