Use python in Linux to call the top command to obtain CPU utilization

Source: Internet
Author: User
This article mainly introduces how to use python to call the top command in Linux to obtain the CPU usage. This article provides the implementation code directly. if you need it, refer to the following content: if you want to use python to call the top command to obtain the cpu usage, you have no idea.
To obtain the cpu utilization, you can use the top command to redirect it easily. the command is as follows:

The code is as follows:


Top-bi> cpuHistory. log


Or

The code is as follows:


Top-bi | tee cpuHistory. log


This will not be explained. if you are not familiar with it, please query the top help document. What we need to achieve here is to call the top command through python and obtain the cpu utilization information.
Friends who have used popen will soon be able to think of code similar to the following (this is the first code I wrote ,*_*):

The code is 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 looks correct, but there is a problem when running: the idle value of the cpu remains unchanged !!!
The reason is that the command "top-bi-n 1" is executed separately. you will find that the cpu idle value in the output result remains unchanged.
So you cannot write this ......
Run the "top-bi-n 2" command on the terminal. you will find that the second value is changing every time. This is what we want to achieve.
Considering the time issue, it would be better to write the command like this: "top-bi-n 2-d 0.02"
The code is as follows:

The code is 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 100 '). read (). split ('\ n \ n') [1]. split ('\ n') [2]
Time. sleep (time2sleep)

The execution result is as follows:

The code is 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

Okay, that's all. I hope it will help 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.