2026-01-29 06:54:10 +01:00
|
|
|
import psutil
|
|
|
|
|
import time
|
|
|
|
|
|
|
|
|
|
try:
|
2026-01-29 07:42:06 +01:00
|
|
|
with open('usage.log', 'a') as log_file:
|
2026-01-29 06:54:10 +01:00
|
|
|
while True:
|
2026-01-29 08:06:31 +01:00
|
|
|
cpu = psutil.cpu_percent(interval=1)
|
2026-01-29 06:54:10 +01:00
|
|
|
mem = psutil.virtual_memory()
|
2026-01-29 08:06:31 +01:00
|
|
|
timestamp = time.strftime('%Y-%m-%d %H:%M:%S')
|
|
|
|
|
log_entry = f"{timestamp} CPU: {cpu}% Memory: {mem.percent}%\n"
|
2026-01-29 07:42:06 +01:00
|
|
|
log_file.write(log_entry)
|
|
|
|
|
log_file.flush()
|
2026-01-29 08:06:31 +01:00
|
|
|
time.sleep(4) # Already waited 1 sec in cpu_percent, so sleep 4 more to total 5
|
2026-01-29 06:54:10 +01:00
|
|
|
except KeyboardInterrupt:
|
2026-01-29 08:06:31 +01:00
|
|
|
print("Monitoring stopped by user.")"}
|