Obtain the CPU usage script.

Source: Internet
Author: User

Obtain the CPU usage script.

#!/bin/bash#gino#20150711CPULOG_1=`cat /proc/stat | head -n1 | awk '{print $2" "$3" "$4" "$5" "$6" "$7" "$8}'`SYS_IDLE_1=`echo $CPULOG_1 | awk '{print $4}'`Total_1=`echo $CPULOG_1 | awk '{print $1+$2+$3+$4+$5+$6+$7}'`sleep 1CPULOG_2=`cat /proc/stat | head -n1 | awk '{print $2" "$3" "$4" "$5" "$6" "$7" "$8}'`SYS_IDLE_2=`echo $CPULOG_2 | awk '{print $4}'`Total_2=`echo $CPULOG_2 | awk '{print $1+$2+$3+$4+$5+$6+$7}'`Total=`expr $Total_2 - $Total_1`SYS_IDLE=`expr $SYS_IDLE_2 - $SYS_IDLE_1`SYS_USAGE=`echo $SYS_IDLE/$Total*100 |bc -l`SYS_Rate=`echo 100-$SYS_USAGE |bc -l`Disp_SYS_Rate=`expr "scale=2; $SYS_Rate/1" |bc`printf "%4.2f%%\n" ${Disp_SYS_Rate}

Summary:
Formula for Calculating the total CPU usage

CPU time = user + system + nice + idle + iowait + irq + softirq + stealCPU time slice = CPU_2-CPU_1IDLE time = idle_2-idle_1CPU usage = 100 * (CPU time slice-IDLE Time) /CPU time slice

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.