Created
April 16, 2022 18:38
-
-
Save shollingsworth/65ef0a2fb5bd8c028229b2cb9a7a635c to your computer and use it in GitHub Desktop.
python3 List all processes, sort by number of open files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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