1

I am using the following command to fetch the CPU usage along with date and time. But I am getting only the CPU usage as output.

My code:

import subprocess
for i in range(3):
    cpu = (subprocess.Popen('top -bn2 -d1 | grep "Cpu\|average" ', shell=True, stdout=subprocess.PIPE).stdout)
    print(cpu, '----')

output:

  %Cpu(s):  7.3 us,  1.2 sy,  0.1 ni, 88.8 id,  2.5 wa,  0.0 hi,  0.1 si,  0.0 st
  %Cpu(s):  1.5 us,  0.2 sy,  0.0 ni, 98.3 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
  --------
  %Cpu(s):  7.3 us,  1.2 sy,  0.1 ni, 88.8 id,  2.5 wa,  0.0 hi,  0.1 si,  0.0 st
  %Cpu(s):  2.7 us,  1.0 sy,  0.0 ni, 93.8 id,  2.5 wa,  0.0 hi,  0.0 si,  0.0 st
  --------
  %Cpu(s):  7.3 us,  1.2 sy,  0.1 ni, 88.8 id,  2.5 wa,  0.0 hi,  0.1 si,  0.0 st
  %Cpu(s):  3.8 us,  0.5 sy,  0.0 ni, 95.7 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st

Expected Output:

 top - 12:58:07 up  2:37,  1 user,  load average: 0.61, 0.72, 0.58
  %Cpu(s):  7.3 us,  1.2 sy,  0.1 ni, 88.9 id,  2.5 wa,  0.0 hi,  0.1 si,  0.0 st
  top - 12:58:08 up  2:37,  1 user,  load average: 0.61, 0.72, 0.58
  %Cpu(s): 12.7 us,  2.0 sy,  0.0 ni, 78.3 id,  7.0 wa,  0.0 hi,  0.0 si,  0.0 st

2 Answers 2

1

Add read() and decode() like this, you will have what you want.

import subprocess
for i in range(3):
    cpu = (subprocess.Popen('top -bn2 -d1 | grep "Cpu\|average" ', shell=True, stdout=subprocess.PIPE).stdout)
    print(cpu.read().decode('utf-8'), '\n----')    

output:

top - 15:43:00 up  7:36,  1 user,  load average: 0,97, 0,95, 1,28
%Cpu(s): 12,4 us,  3,1 sy,  0,0 ni, 84,1 id,  0,0 wa,  0,0 hi,  0,3 si,  0,0 st
top - 15:43:01 up  7:36,  1 user,  load average: 0,97, 0,95, 1,28
%Cpu(s):  5,9 us,  1,5 sy,  0,0 ni, 92,5 id,  0,0 wa,  0,0 hi,  0,2 si,  0,0 st

----
top - 15:43:01 up  7:36,  1 user,  load average: 0,97, 0,95, 1,28
%Cpu(s): 12,4 us,  3,1 sy,  0,0 ni, 84,1 id,  0,0 wa,  0,0 hi,  0,3 si,  0,0 st
top - 15:43:02 up  7:36,  1 user,  load average: 0,89, 0,94, 1,27
%Cpu(s):  2,1 us,  0,8 sy,  0,0 ni, 96,9 id,  0,0 wa,  0,0 hi,  0,2 si,  0,0 st

----
top - 15:43:02 up  7:36,  1 user,  load average: 0,89, 0,94, 1,27
%Cpu(s): 12,4 us,  3,1 sy,  0,0 ni, 84,1 id,  0,0 wa,  0,0 hi,  0,3 si,  0,0 st
top - 15:43:03 up  7:36,  1 user,  load average: 0,89, 0,94, 1,27
%Cpu(s):  4,1 us,  0,8 sy,  0,0 ni, 94,9 id,  0,0 wa,  0,0 hi,  0,2 si,  0,0 st

----

Sign up to request clarification or add additional context in comments.

Comments

0

The output of the stdout is of type BufferedReader, you need to either add .read() method or .communicate() to get proper string output, like :

cpu = (subprocess.Popen('top -bn2 -d1 | grep "Cpu\|average" ', shell=True, stdout=subprocess.PIPE).stdout).read()

or using communicate ( here you will also get errors )

cpu, err = subprocess.Popen('top -bn2 -d1 | grep "Cpu\|average" ', shell=True, stdout=subprocess.PIPE).communicate()

here is the output :

print(cpu.decode('utf-8'))
'top - 14:22:27 up  4:24,  1 user,  load average: 1.40, 1.72, 1.80\n%Cpu(s): 12.6 us,  3.2 sy,  1.8 ni, 59.9 id, 22.3 wa,  0.0 hi,  0.2 si,  0.0 st\ntop - 14:22:28 up  4:24,  1 user,  load average: 1.45, 1.72, 1.80\n%Cpu(s):  3.0 us,  2.5 sy,  0.0 ni, 83.6 id, 10.9 wa,  0.0 hi,  0.0 si,  0.0 st\n'

more info at docs : here

1 Comment

Thanks for clarifying my doubt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.