Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

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 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,

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
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


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.