Created
November 25, 2015 09:23
-
-
Save gmastrokostas/c0066d15b8dd6f684a1b to your computer and use it in GitHub Desktop.
Python – Get list of processes, their owners and RAM usage from a Linux system.
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
| import os, sys | |
| from os.path import join,getsize | |
| import humanize | |
| import pwd | |
| import psutil | |
| pids = [int(pid) for pid in os.listdir('/proc') if pid.isdigit()] | |
| for elements in pids: | |
| p = psutil.Process(elements) | |
| proc_name = p.name() | |
| proc_stat_file = os.stat("/proc/%d" % elements) | |
| uid = proc_stat_file.st_uid | |
| username = pwd.getpwuid(uid)[0] | |
| human_size = humanize.naturalsize(p.memory_info_ex()[0], gnu=True) | |
| #print p.memory_info_ex()[0] | |
| #print username,"\t\t", elements,"\t\t", p.name(), "\t\t", human_size | |
| print username.ljust(20), elements, p.name(), human_size.rjust(20) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment