Python – Get list of processes, their owners and RAM usage from a Linux system.

This script only works on Linux systems. It lists the running PIDs, the owners of the PIDs and the RAM usage of each PID.

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)

Output Example

root                 17601 cupsd                 3.4M
gmastrokostas        23460 chrome               117.5M
gmastrokostas        24034 pithos                76.2M
Share Button

Python – Get a listing of all subdirectories and their size on a Linux system

The script asks for a directory from the user. It then will dive in all the subdirectories within that directory. It will list all subdirectories and their size. It does this by actually looking for files within the subdirectories and calculating the actual size (not block size) of all the files with in that subdirectory.

#!/usr/bin/python
import humanize
import os, sys
from os.path import join,getsize
import humanize
import pwd

def dir_list():
    list = []
    drct = raw_input(":Enter directory name. Use full path: ")
    for dirpath, dirnames, filenames in os.walk(drct, followlinks=False):
        for loop_dir in dirnames:
            path = os.path.join(dirpath, loop_dir) #Joins the names of directories with the actual path
            list.append(path) # Enters all the directories with the full path to a list

    return list  #Return the whole list so dir_size function can process it

def dir_size():
    
    returned_list = dir_list() #The list returned from the dir_list function

    for loop in returned_list: #Breaks down the list in strings in order for OS.WALK to be able to process it
        total = 0
        for dirpath, dirnames, filenames in os.walk(loop, followlinks= False): #grab the paths from the list. This is the same as asking a user to enter a path

            for f_name in filenames: #Dive into directories to view files
                path = os.path.join(dirpath, f_name) #Join path with filenames for the directory we are trying to find the size
                if os.path.islink(path): 
                    continue
                else:
                    total += os.path.getsize(path)  #Find size of actual files within the directory
                    human_size = humanize.naturalsize(total, gnu=True) 
        print "Size of directory  : ",loop, "is", human_size
        human_size = 0

dir_size()

OutPut Example

FUN 1:Enter directory name. Use full path: /home/gmastrokostas
Size of directory  :  /home/gmastrokostas/.mozilla is 16.1M
Size of directory  :  /home/gmastrokostas/.config is 69.9K
Size of directory  :  /home/gmastrokostas/.cache is 53.0M
Size of directory  :  /home/gmastrokostas/Desktop is 0
Size of directory  :  /home/gmastrokostas/Downloads is 332.9M
Size of directory  :  /home/gmastrokostas/Templates is 0
Size of directory  :  /home/gmastrokostas/Public is 0
Size of directory  :  /home/gmastrokostas/Documents is 218.6M
Size of directory  :  /home/gmastrokostas/Music is 0
Size of directory  :  /home/gmastrokostas/Pictures is 0
Size of directory  :  /home/gmastrokostas/Videos is 0
Size of directory  :  /home/gmastrokostas/.local is 1.2M
Share Button