Top like interface for lsof

If you’re curious which pids are using loads of open files this script displays just that.

Example:

The following overview will update +/- every second.

      OPEN / LIMIT      NAME
     32568 / 4096       chrome          (PID  1365)
     14473 / 4096       spotify         (PID  1071)
     12530 / 4096       nylas           (PID  8430)
     10952 / 4096       skypeforlinux   (PID 26476)
      9471 / 4096       skypeforlinux   (PID 26513)
      5360 / 4096       Nylas_Mail_defa (PID  8914)
      4998 / 4096       gnome-shell     (PID   823)
      3726 / 4096       Nylas_Mail_work (PID  8473)
      3528 / 4096       spotify         (PID  1222)
      3105 / 4096       Enpass          (PID  1065)
      2898 / 4096       chrome          (PID  1450)
      2864 / 4096       Nylas_Mail_empt (PID  6152)
      2752 / 4096       tracker-extract (PID  1076)
      2528 / 4096       telegram-deskto (PID 29313)
      1908 / 4096       TeamViewer.exe  (PID 17517)
      1480 / 4096       nylas           (PID 27110)
      1480 / 4096       nylas           (PID 27099)
      1480 / 4096       nylas           (PID 27092)
      1132 / 4096       nylas           (PID  8458)
       890 / 4096       spotify         (PID  1207)
       870 / 4096       gsd-media-keys  (PID  1000)
       858 / 4096       TVGuiSlave.64   (PID 17658)
       845 / 4096       skypeforlinux   (PID 26501)
       810 / 4096       TVGuiDelegate   (PID 17659)
       810 / 4096       solaar          (PID  1070)

lsof-top

#!/bin/bash
# It is pretty clear this is a crude way to do it, and yes it started as a quick one liner.


while true; do
  RESULT=$(
    lsof -n 2>/dev/null | awk '{print $2}' | sort | uniq -c | sort -nr | head -25 | while read nr pid ; do
    printf "%10d / %-10d %-15s (PID %5s)\n" $nr $(cat /proc/$pid/limits | grep 'open files' | awk '{print $5}') $(ps -p $pid -o comm= | sed 's/\ /_/g' ) $pid
    done
  )
  clear
  echo "      OPEN / LIMIT      NAME"
  echo -n "$RESULT"
  sleep 1
done

How it works:

  • list all open files
  • clean the lines just keep the pid
  • count the occurences of pid in the open file list
  • create a line for the top 25 open file pids in a RESULT var
  • clear the screen
  • echo the RESULT
Cloud & Open-Source magician 🧙‍♂️

I try to find the KISS in complex systems and share it with the world.

comments powered by Disqus