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.

Share:
5,815

Related videos on Youtube

sawa
Author by

sawa

Updated on September 18, 2022

Comments

  • sawa
    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.
      Hanan N. over 12 years
      lsof -i list just the network connections.
    • sawa
      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
      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
      sawa over 12 years
      @Marcin Your information was actually helpful as much as the answers given. Thank you.
    • Nils
      Nils over 12 years
      Is there any reason why you cant use killall` pgrep or pkill?
    • sawa
      sawa over 12 years
      @Nils Yes. I can't narrow down what I want to terminate just by the process name.
  • Nikhil Mulley
    Nikhil Mulley over 12 years
    I understand to use -p option, Linux requires you to be root user.
  • Matteo
    Matteo over 12 years
    It's definitely faster since netstat only lists network connections (and not all the open files)
  • sawa
    sawa over 12 years
    Thank you for the quick and accurate response. I helped me a lot.
  • sawa
    sawa over 12 years
    Yes, that seems to sove the problem, and is helpful. It was partly suggested by Marcin in a comment to my answer.
  • user1133275
    user1133275 about 6 years
    time reported lsof taking 10 seconds regardless of the n option.