Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to count CPU utilization through shell script in Linux system

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how the Linux system calculates CPU utilization through shell scripts. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

The concrete realization method of shell script: 1. Input top-bn1 (b controls the output format to text format, non-interactive format)

[mysql@jbxue] $top-bn12. Enter top to enter interactive mode to prepare for the next step of setting the output format

[mysql@jbxue] $top and then enter 1

You can view multiple CPU utilization:

Top-21:40:51 up 25 days, 9:32, 1 user, load average: 0.15,0.30,0.33`Tasks: 101total, 1 running, 96 sleeping, 0 stopped, 4 zombie`Cpu0: 0.8% us, 0.3% sy, 0.0% ni, 98.4% id, 0.3% wa, 0.0% hi, 0.0% si`Cpu1: 1.1% us, 0.2% sy, 0.0% ni, 98.6% id, 0.1% wa, 0.0% hi 0% si``Cpu2: 0.5% us, 0.1% sy, 0.0% ni, 99.4% id, 0.0% wa, 0.0% hi, 0.0% si`Cpu3: 0.4% us, 0.1% sy, 0.0% ni, 99.5% id, 0.0% wa, 0.0% hi, 0.0% si`Cpu4: 0.3% us, 0.1% sy, 0.0% ni, 99.5% id, 0.0% wa, 0.0% hi 0% si``Cpu5: 0.3% us, 0.1% sy, 0.0% ni, 99.6% id, 0.0% wa, 0.0% hi, 0.0% si`Cpu6: 0.3% us, 0.1% sy, 0.0% ni, 99.6% id, 0.0% wa, 0.0% hi, 0.0% si`Cpu7: 0.2% us, 0.1% sy, 0.0% ni, 99.7% id, 0.0% wa, 0.0% hi, 0.0% si3. Save the parameters of the current output format (configure output) press W on the basis of 2, and then press ctri+c

Save exit press Q (there will be a .toprc file under / home/mysql)

4. Write shell program cpu.sh

#! / bin/bash while true do top-bcisSn1 > > cpu.log sleep 10 done5. Deploy the cpu.sh program

Sh cpu.sh & III. By analyzing the log cat cpu.log | grep cpu and other operations, you can analyze the past time and the utilization of each cpu. Omit here... ..., according to personal preferences, output, and even the output can be drawn with cacti.

four。 Delete profile if you do not want to use the above top output format, you can delete the profile

Rm / home/mysql/.toprc on the Linux system how to count CPU utilization through the shell script to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report