Sunday, May 5, 2024
 Popular · Latest · Hot · Upcoming
68
rated 0 times [  68] [ 0]  / answers: 1 / hits: 36002  / 3 Years ago, mon, september 13, 2021, 1:35:50

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.


More From » bash

 Answers
5

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)

[#29403] Monday, September 13, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
fulerio

Total Points: 172
Total Questions: 124
Total Answers: 109

Location: Hungary
Member since Thu, Aug 6, 2020
4 Years ago
fulerio questions
;