Skip to content

Instantly share code, notes, and snippets.

@huang4207100
Last active November 8, 2019 02:16
Show Gist options
  • Select an option

  • Save huang4207100/23100f2492d5fdafa636b4bedcc875cf to your computer and use it in GitHub Desktop.

Select an option

Save huang4207100/23100f2492d5fdafa636b4bedcc875cf to your computer and use it in GitHub Desktop.
[python监控] python监控进程的CPU和内存 #python #psutil
import sys
import time
import psutil
# get pid from args
if len(sys.argv) < 2:
print ("missing pid arg")
sys.exit()
# get process
pid = int(sys.argv[1])
p = psutil.Process(pid)
# monitor process and write data to file
interval = 3 # polling seconds
with open("process_monitor_" + p.name() + '_' + str(pid) + ".csv", "a+") as f:
f.write("time,cpu%,mem%\n") # titles
while True:
current_time = time.strftime('%Y%m%d-%H%M%S',time.localtime(time.time()))
cpu_percent = p.cpu_percent()
mem_percent = p.memory_percent()
line = current_time + ',' + str(cpu_percent) + ',' + str(mem_percent)
print (line)
f.write(line + "\n")
time.sleep(interval)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment