-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpu.py
More file actions
27 lines (21 loc) · 678 Bytes
/
cpu.py
File metadata and controls
27 lines (21 loc) · 678 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/usr/bin/env python3
try:
import sys, os, time
import psutil
print('Module is imported')
except ModuleNotFoundError:
print('Install the Module')
sys.exit(1)
# Calling psutil.cpu_precent() for 4 seconds
count = 1
while count < 5:
print('The CPU usage is: ', psutil.cpu_percent(4))
print('RAM memory % used:', psutil.virtual_memory()[2])
print("Completed the cycle")
time.sleep(2)
count += 1
# Getting all memory using os.popen()
total_memory, used_memory, free_memory = map(
int, os.popen('free -t -m').readlines()[-1].split()[1:])
# Memory usage
print("RAM memory % used:", round((used_memory/total_memory) * 100, 2))