首页 > 代码库 > shell监控之列出1小时内cpu占用最多的10个进程

shell监控之列出1小时内cpu占用最多的10个进程

脚本内容如下:

-------------------------------------------------------------------------------------------------------

#!/bin/sh

#定义变量

secs=3600
unit_time=60

stepts=$(( $secs / $unit_time ))

echo CPU usage...;

for((i=0;i<stepts;i++))
do
    ps -eo comm,pcpu | tail -n +2 >> /tmp/cpu_usage.$$                          #$$表示当前进程pid信息
    sleep $unit_time
done

echo CPU eaters:

cat /tmp/cpu_usage.$$ | \
awk ‘{process[$1]+=$2}END{for(i in process){printf("%-20s %s\n",i,process[i])}}‘ | \                          #使用awk对收集结果进行统计分析
sort -nrk 2 | head

rm -f /tmp/cpu_usage.$$

-------------------------------------------------------------------------------------------------------------

shell监控之列出1小时内cpu占用最多的10个进程