Tuesday, May 14, 2024
 Popular · Latest · Hot · Upcoming
3
rated 0 times [  3] [ 0]  / answers: 1 / hits: 619  / 1 Year ago, thu, december 8, 2022, 4:08:27

I always want to kill a process id which listening on Port 3000.
So presently I am using,



1.lsof -i tcp:3000


will give output as,



COMMAND  PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
ruby 6697 xxxx 5u IPv4 251982 0t0 TCP *:3000 (LISTEN)


and I will kill the PID using,

2. kill -9 6697



I want to create a bash program which do this process automatically.



I tried using xargs to get PID of the output of first command but failed.


More From » bash

 Answers
3

First, you want the -t option to lsof, which only emits pids. You can give kill a list of pids too, so:



kill -9 $(lsof -ti tcp:3000)


If you do this a lot, make an alias



alias kill3000='kill -9 $(lsof -ti tcp:3000)'

[#43214] Saturday, December 10, 2022, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tocklftime

Total Points: 110
Total Questions: 109
Total Answers: 100

Location: Mayotte
Member since Mon, Sep 12, 2022
2 Years ago
tocklftime questions
Wed, Feb 1, 23, 21:50, 1 Year ago
Tue, Oct 4, 22, 21:42, 2 Years ago
Sun, Jul 25, 21, 10:43, 3 Years ago
;