Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. 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 want to extract the process id of a certain process in order to shut it down. The process is a local web server using a certain port (localhost:3000), and I am currently doing it like this to extract the relevant line:

lsof|grep localhost:3000

but the lsof command is too slow. Is there a faster way to extract the process id?

share|improve this question
lsof -i list just the network connections. – Hanan N. Nov 30 '11 at 10:44
@HananN. I tried it, but it is still very slow. Using netstat as suggested in the answers is much faster. – sawa Nov 30 '11 at 11:06
lsof is not the slow part, using all the name resolutions and ip resolving is. Make it all numeric and it will go fast. – Marcin Nov 30 '11 at 14:29
@Marcin Your information was actually helpful as much as the answers given. Thank you. – sawa Nov 30 '11 at 16:17
Is there any reason why you cant use killall` pgrep or pkill? – Nils Dec 1 '11 at 20:28
up vote 7 down vote accepted

Try netstat, I cannot say whether its faster or slower, however.

netstat -tanp | awk '$4 ~ /:8443$/ {sub(/\/.*/, "", $7); print $7}' | sort -u
share|improve this answer
I understand to use -p option, Linux requires you to be root user. – Nikhil Mulley Nov 30 '11 at 9:49
It's definitely faster since netstat only lists network connections (and not all the open files) – Matteo Nov 30 '11 at 9:50
Thank you for the quick and accurate response. I helped me a lot. – sawa Nov 30 '11 at 10:00

You can put a -n option to lsof and then it remove the DNS resolution, which can accelerate the display

share|improve this answer
Yes, that seems to sove the problem, and is helpful. It was partly suggested by Marcin in a comment to my answer. – sawa Dec 1 '11 at 20:15
sudo ss -p -l '( sport = :3000 )'|awk -F"," 'NR!=1{print $2}'

This will print the PID of the process listening on 3000

time reports that this completed in 0m0.018s.

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.