Skip to content

Instantly share code, notes, and snippets.

@shollingsworth
Created April 16, 2022 18:38
Show Gist options
  • Save shollingsworth/65ef0a2fb5bd8c028229b2cb9a7a635c to your computer and use it in GitHub Desktop.
Save shollingsworth/65ef0a2fb5bd8c028229b2cb9a7a635c to your computer and use it in GitHub Desktop.
python3 List all processes, sort by number of open files
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""List all processes, sort by number of open files."""
from collections import Counter
import psutil
def main():
"""Run main function."""
cntr = Counter()
pmap = {}
for i in psutil.pids():
proc = psutil.Process(i)
name = proc.name()
pmap[name] = proc
try:
pcnt = proc.num_fds()
cntr[name] += pcnt
except psutil.AccessDenied:
pass
total = sum(cntr.values())
print(f"Total number of file descriptors: {total}")
for k, v in cntr.most_common():
proc = pmap[k]
print(f"{k:<20} {v:>5} pid:{proc.pid}")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment