Faster alternatives to lsof
5,815
Solution 1
Try netstat, I cannot say whether its faster or slower, however.
netstat -tanp | awk '$4 ~ /:8443$/ {sub(/\/.*/, "", $7); print $7}' | sort -u
Solution 2
You can put a -n option to lsof and then it remove the DNS resolution, which can accelerate the display
Solution 3
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.
Related videos on Youtube
Author by
sawa
Updated on September 18, 2022Comments
-
sawa over 1 year
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?-
Hanan N. over 12 years
lsof -i
list just the network connections. -
sawa over 12 years@HananN. I tried it, but it is still very slow. Using netstat as suggested in the answers is much faster.
-
BrettRobi over 12 years
lsof
is not the slow part, using all the name resolutions and ip resolving is. Make it all numeric and it will go fast. -
sawa over 12 years@Marcin Your information was actually helpful as much as the answers given. Thank you.
-
Nils over 12 yearsIs there any reason why you can
t use
killall`pgrep
orpkill
? -
sawa over 12 years@Nils Yes. I can't narrow down what I want to terminate just by the process name.
-
-
Nikhil Mulley over 12 yearsI understand to use -p option, Linux requires you to be root user.
-
Matteo over 12 yearsIt's definitely faster since netstat only lists network connections (and not all the open files)
-
sawa over 12 yearsThank you for the quick and accurate response. I helped me a lot.
-
sawa over 12 yearsYes, that seems to sove the problem, and is helpful. It was partly suggested by Marcin in a comment to my answer.
-
user1133275 about 6 yearstime reported lsof taking 10 seconds regardless of the n option.