Created
June 25, 2016 07:54
-
-
Save izmailoff/be2397e8eb40b367ca7a01308aa77d6a to your computer and use it in GitHub Desktop.
Cross-platform Python script for collecting CPU and memory usage
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
# Prints % of total CPU and memory used in the system | |
# Requires psutil. Install with: | |
# `pip install psutil` | |
# or something like: | |
# `dnf install python3-psutil` | |
# Run with: | |
# `python3 profile_system.py` | |
# Ctrl + C to exit | |
import time | |
import psutil | |
import sys | |
intervalSec = 2 | |
def printStats(): | |
print ("{}; {}".format(psutil.cpu_percent(interval=None), psutil.virtual_memory().percent)) | |
def printLoop(): | |
try: | |
while True: | |
printStats() | |
time.sleep(intervalSec) | |
except KeyboardInterrupt: | |
print ("User exit...", file=sys.stderr) | |
sys.exit(0); | |
if (len(sys.argv) > 1): | |
try: | |
intervalSec = int(sys.argv[1]) | |
except ValueError: | |
print ("usage: ", sys.argv[0], " <refresh interval in seconds>") | |
sys.exit(1) | |
printLoop() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment