linux脚本检查cpu,几个检测cpu信息的shell脚本(图文)

一、判断cpu是否繁忙

代码如下:

复制代码 代码示例:

#!/bin/bash

#filename chk_cpu.sh

#edit by www.jquerycn.cn

busy()

{

touch cpu.txt;

echo -e "\033[31m \n Detecting the %idle of CPU ... \033[0m ";

export temp=1;

sar -u 1 3 | awk '{print$9}' | tail -n 4 >>cpu.txt ;

tail -n 4 cpu.txt | cut -d "." -f 1 | while read var;

do

#echo $var;

if [[ $var -ge 20 ]];then

delete; ### Run into delete function

exit;

fi

temp=$[ $temp + 1 ];

if [ $temp -eq 4 ];then

echo -e "\n \033[37;5m So sorry, System is busy now ! \n\033[0m ";

fi

done;

rm -rf cpu.txt;

}

#use busy()

busy()

代码说明:

以上脚本用到了sar命令,有关这个命令的用法,请参考文章:

另外,注意下033[31m \033[0m这样的字眼,这是用来设置字体或背景颜色的。

相关内容,可以参考文章:

调用示例:

a_9313

二,统计cpu与内存的使用率

代码如下:

复制代码 代码示例:

#!/bin/sh

#filename chk_cpu_memory.sh

#edit by www.jquerycn.cn

clear

while ((1>0))

do

NET=$(netstat -anp | grep 1000 | grep EST | wc -l)

PRC=$(ps aux | grep thread1 | grep -v grep | grep -v SCREEN | awk '{ print " CPU: " $3 "%,  MEM: " $4 "%, RSS: " $6/1024 "M, VSZ: " $5/1024 "M, PID: "

$2 ""}')

tput sc; echo -n -e "\b"; tput rc;

echo "SRV:  thread1, NET: $NET, $PRC"

echo ""

NET=$(netstat -anp | grep 1011 | grep EST | wc -l)

PRC=$(ps aux | grep thread2 | grep -v grep | grep -v SCREEN | awk '{ print " CPU: " $3 "%, MEM: " $4 "%, RSS: " $6/1024 "M, VSZ: " $5/1024 "M, PID: "

$2 ""}')

tput sc; echo -n -e "\b"; tput rc;

echo "SRV:  thread2, NET: $NET, $PRC"

echo ""

NET=$(netstat -anp | grep 1022 | grep EST | wc -l)

PRC=$(ps aux | grep thread3 | grep -v grep | grep -v SCREEN | awk '{ print " CPU: " $3 "%, MEM: " $4 "%, RSS: " $6/1024 "M, VSZ: " $5/1024 "M, PID: "

$2 ""}')

tput sc; echo -n -e "\b"; tput rc;

echo "SRV:  thread3, NET: $NET, $PRC"

echo ""

NET=$(netstat -anp | grep 1033 | grep EST | wc -l)

PRC=$(ps aux | grep thread4 | grep -v grep | grep -v SCREEN | awk '{ print " CPU: " $3 "%, MEM: " $4 "%, RSS: " $6/1024 "M, VSZ: " $5/1024 "M, PID: "

$2 ""}')

tput sc; echo -n -e "\b"; tput rc;

echo "SRV:  thread4, NET: $NET, $PRC"

echo ""

NET=$(netstat -anp | grep beam.smp | grep EST | grep 222.222.18.222:800 | wc -l)

PRC=$(ps aux | grep thread | grep -v grep | awk '{CPU += $3} {MEM += $4} {RSS += $6} {VSZ += $5}  END {print "CPU: " CPU "%, MEM: " MEM "%, RSS: "

RSS/1024 "M, VSZ: " VSZ/1024 "M,  "  " "}'  | awk 'END {print}')

tput sc; echo -n -e "\b"; tput rc;

echo "SRV: All, NET: $NET, $PRC"

echo ""

sleep 0.5

tput cup 0 0

done

调用示例:

1405434524_1212.jpg

三,获取CPU信息

分析/proc/cpuinfo中有关CPU的信息,然后构造一个清晰的CPU信息状态输出,直观地看出当前CPU的相关内容。

在文件/proc/cpuinfo中,注意以下信息项:

processor 逻辑CPU号(核或线程)

core id  CPU核号

physical id 物理CPU号

脚本内容如下:

复制代码 代码示例:

#!/bin/bash

#filename get_cpu_info.sh

#edit by www.jquerycn.cn

cpu_p=`cat /proc/cpuinfo | grep "physical id" | sort | uniq | wc -l`

cpu_c=`cat /proc/cpuinfo | grep "core id" | uniq | wc -l`

cpu_l=`cat /proc/cpuinfo | grep "processor" | wc -l`

c_p=`expr $cpu_c / $cpu_p`

l_p=`expr $cpu_l / $cpu_p`

t_c=`expr $cpu_l / $cpu_c`

echo "Physical CPU Count: $cpu_p"

echo "CPU Core Count   : $cpu_c"

echo "Logical CPU Count: $cpu_l"

echo "$c_p cores per physical CPU"

echo "$l_p logical CPUs per physical CPU"

echo "$t_c threads per core"

if [ $c_p -eq 1 ]; then

echo "Your CPU is unicore"

else

echo "Your CPU is multicore"

fi

if [ $t_c -eq 1 ]; then

echo "Your CPU isn\'t of HT"

else

echo "Your CPU is of HT"

fi

echo "You have $cpu_p CPUs, each CPU has $c_p cores, each core has $t_c threads. In summary $cpu_c cores, $cpu_l threads."

调用示例:

1405434524_1713.jpg

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值