简体   繁体   中英

Python CPU and RAM memory usage

I am writing a script that writes CPU and RAM usage, I want to add list every process using 1% CPU or RAM

import datetime ,psutil ,time ,subprocess

mem_usage = (psutil.virtual_memory())
cpu_usage = psutil.cpu_percent(interval=1)
cmd = "WMIC PROCESS get Caption,ProcessId"
proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)

d_h = datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S")
f = open('dane.txt', 'a')
f.write("DATE: "+d_h+'\n')
f.write("MEM USAGE: "+(str(mem_usage)+'\n'))
f.write("CPU USAGE: "+(str(cpu_usage)+"%"'\n'))
f.write("CPU PROCESS LIST: "+""+'\n')
for proc in psutil.process_iter():
    f.write(str(proc.name)+'\n')
f.write(" "+'\n')
f.close()

I would like it to look like this

file.txt

DATE & TIME
CPU_USAGE: 45%
MEM_USAGE: 88%
PROCESS_LIST:
list all process usage 1% RAM or CPU

thanks in advance

for proc in psutil.process_iter():
    f.write(str(proc.name())+'\n')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM