1

Hello i want to get CPU information continuously but i can only get one with this code how can i fix it ?

import subprocess
import time 
while 1:
       top = subprocess.Popen(['top','-b','n1'],stdout = subprocess.PIPE)
       grep = subprocess.Popen(['grep','-i','Cpu(s)'] stdin = top.stdout, stdout = subprocess.PIPE , stderr = subprocess.STDOUT)
       cut = subprocess.Popen(['cut','-c','-13'] , stdin = grep.stdout , stdout = subprocess.PIPE)
       print output
       time.sleep(3)
Fredrik Pihl
  • 44,604
  • 7
  • 83
  • 130
  • see e.g. http://stackoverflow.com/questions/276052/how-to-get-current-cpu-and-ram-usage-in-python – Fredrik Pihl Mar 24 '15 at 13:50
  • you could use `psutil` module as [@FredrikPihl suggested](http://stackoverflow.com/questions/29234354/cpu-from-top-command#comment46674701_29234354), to get cpu information. But if you want to get the output of the `top` command specifically; see [Stop reading process output in Python without hang?](http://stackoverflow.com/q/4417962/4279) and [How do I use subprocess.Popen to connect multiple processes by pipes?](http://stackoverflow.com/q/295459/4279) – jfs Mar 24 '15 at 19:01

0 Answers0