Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 3 down vote accepted

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)'
share|improve this answer
    
:) with help of your command I created with xargs alias kil='lsof -ti tcp:3000 | xargs kill -9' –  Abdul Shajin Oct 4 '11 at 19:28

Here it is (without xargs):

lsof -i tcp:8080 | while read cmd pid rest; do ((pid)) && kill -9 $pid; done
share|improve this answer

Your Answer

 
discard

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.