1

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

2
  • 1
    which output are you currently getting? Commented Dec 4, 2018 at 17:11
  • in file.txt write DATE: 2018/11/29 12:23:39 MEM USAGE: svmem(total=8483639296, available=3379355648, percent=60.2, used=5104283648, free=3379355648) CPU USAGE: 76.4% CPU PROCESS LIST: <bound method Process.name of psutil.Process(pid=0, name='System Idle Process', started='2018-11-15 10:02:47')> Commented Dec 4, 2018 at 17:20

1 Answer 1

1
for proc in psutil.process_iter():
    f.write(str(proc.name())+'\n')
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.