Skip to content

Instantly share code, notes, and snippets.

@btbytes
Created July 9, 2010 01:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save btbytes/468934 to your computer and use it in GitHub Desktop.
Save btbytes/468934 to your computer and use it in GitHub Desktop.
Simple module for getting amount of memory used by a specified user's processes on a UNIX system.
"""Simple module for getting amount of memory used by a specified user's
processes on a UNIX system.
It uses UNIX ps utility to get the memory usage for a specified username and
pipe it to awk for summing up per application memory usage and return the total.
Python's Popen() from subprocess module is used for spawning ps and awk.
source: http://stackoverflow.com/questions/276052/how-to-get-current-cpu-and-ram-usage-in-python
"""
import subprocess
class MemoryMonitor(object):
def __init__(self, username):
"""Create new MemoryMonitor instance."""
self.username = username
def usage(self):
"""Return int containing memory used by user's processes."""
self.process = subprocess.Popen("ps -u %s -o rss | awk '{sum+=$1} END {print sum}'" % self.username,
shell=True,
stdout=subprocess.PIPE,
)
self.stdout_list = self.process.communicate()[0].split('\n')
return int(self.stdout_list[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment