Use Python to call the top command for CPU utilization under Linux

Source: Internet
Author: User
This article locates the situation where you want to call the top command through Python to get CPU usage but have no idea at the moment.
If you simply want to get CPU utilization, the top command redirection can be easily implemented with the following command:
Copy CodeThe code is as follows:


Top-bi > CpuHistory.log


Or
Copy CodeThe code is as follows:


Top-bi | Tee CpuHistory.log


This does not explain, do not understand the friend query top of the help document. What is accomplished here is to call the top command through Python and get the CPU utilization information.
Friends with Popen will soon be able to think of code similar to the following (this is the first time I wrote the Code, *_*):

Copy the Code code as follows:


#! /usr/bin/python

Import Os,time

Time2sleep = 1.5
While True:
Print Os.popen (' Top-bi-n 1 '). Read (). split (' \ n ') [2]
Time.sleep (Time2sleep)

The principle seems to be correct, but run up the problem: the idle value of the CPU has been constant!!!
The reason is that the command executed "Top-bi-n 1": Execute this command separately, you will find that the output of the CPU idle value is constant.
So you can't write like this ...
Executing the "Top-bi-n 2" command at the terminal, you will find that the second value changes every time, and this is what we want the results to be.
Given the time, the order would be better: "Top-bi-n 2-d 0.02"
The code is as follows:

Copy the Code code as follows:


#! /usr/bin/python
'''
File:cpuRate.py
Author:mike
E-mail:mike_zhang@live.com
'''
Import Os,time

Time2sleep = 2.5
While True:
print int (time.time ()),
Print Os.popen (' top-bi-n 2-d 0.02 '). Read (). Split (' \n\n\n ') [1].split (' \ n ') [2]
Time.sleep (Time2sleep)

The following results are performed:

Copy the Code code as follows:


$./cpurate.py
1328109437 Cpu (s): 10.0%us, 20.0%sy, 0.0%ni, 70.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
1328109441 Cpu (s): 0.0%us, 16.7%sy, 0.0%ni, 83.3%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
1328109444 Cpu (s): 0.0%us, 16.7%sy, 0.0%ni, 83.3%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
1328109447 Cpu (s): 12.5%us, 12.5%sy, 0.0%ni, 75.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st

All right, that's it, I hope it helps you.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.