• 获取指定进程在一段时间内cpu和内存的最大最小值【调试版】


     1 #!/bin/bash
     2 process=(iss_server DbsServer.out)
     3  
     4 x=0
     5 while true 
     6 do 
     7     i=0
     8     while (($i<${#process[*]}))
     9     do 
    10         
    11         pname=${process[$i]}
    12         pn=`echo ${pname:0:3}|tr '[A-Z]' '[a-z]'`
    13 
    14         pid=`ps -ef|grep $pname|grep -v grep|awk '{print $2}'`
    15         cpu=`ps -p $pid -o pcpu --no-headers|sed "s/[[:space:]]//g"`
    16         mem=`ps -p $pid -o pmem --no-headers|sed "s/[[:space:]]//g"`
    17         if [ $x == 0 ]
    18         then
    19             echo `date`>>${pn}_cpumem.txt
    20             echo "$pname:pid=$pid,maxcpu=$cpu,mincpu=$cpu,maxmem=$mem,minmem=$mem;">>${pn}_cpumem.txt        
    21             eval ${pn}_maxcpu=$cpu
    22             eval ${pn}_mincpu=$cpu
    23             eval ${pn}_maxmem=$mem
    24             eval ${pn}_minmem=$mem
    25             eval ${pn}_pid=$pid
    26         else
    27             rpid=`eval echo '$'${pn}_pid`
    28             rmaxcpu=`eval echo '$'${pn}_maxcpu`
    29             rmincpu=`eval echo '$'${pn}_mincpu`
    30             rmaxmem=`eval echo '$'${pn}_maxmem`
    31             rminmem=`eval echo '$'${pn}_minmem`
    32             if [ $rpid != $pid ];then
    33                 eval ${pn}_pid=$pid
    34                 echo "$pname:pid changed,now pid=$pid">>${pn}_cpumem.txt        
    35             fi
    36             if [ `echo $cpu\>$rmaxcpu | bc` -eq 1 ];then
    37                 eval ${pn}_maxcpu=$cpu
    38                 echo "$pname:pid=$pid,maxcpu=$cpu;">>${pn}_cpumem.txt        
    39             elif [ `echo $cpu\<$rmincpu | bc` -eq 1 ];then
    40                 eval ${pn}_mincpu=$cpu
    41                 echo "$pname:pid=$pid,mincpu=$cpu;">>${pn}_cpumem.txt        
    42             fi
    43             if [ `echo $mem\>$rmaxmem | bc` -eq 1 ];then
    44                 eval ${pn}_maxmem=$mem
    45                 echo "$pname:pid=$pid,maxmem=$mem;">>${pn}_cpumem.txt        
    46             elif [ `echo $mem\<$rminmem | bc` -eq 1 ];then
    47                 eval ${pn}_minmem=$mem
    48                 echo "$pname:pid=$pid,minmem=$mem;">>${pn}_cpumem.txt        
    49             fi
    50         fi
    52         i=`expr $i+1`
    53     done
    54         x=1
    55     sleep 10
    56 done

    1、使用数组存储指定进程,这里指定了两个进程;

    process=(iss_server DbsServer.out)

    2、使用命令分别获取进程pid、cpu和内存,其中sed "s/[[:space:]]//g"是为了去除值中的空格,比如进程cpu是2.3%,取出值【 2.3】前面含有空格,导致后面赋值语句报错;

    1 pid=`ps -ef|grep $pname|grep -v grep|awk '{print $2}'`
    2 cpu=`ps -p $pid -o pcpu --no-headers|sed "s/[[:space:]]//g"`
    3 mem=`ps -p $pid -o pmem --no-headers|sed "s/[[:space:]]//g"`

    3、这里的x是为了判断是否首次获取,(=0是首次,=1非首次,见第4行和54行)如果是首次获取,则给各进程的最大值和最小值赋初值,其中,eval命令是为了二次解析复杂变量,如果没有eval,只有等号右边的值能被解析,${pn}就不能解析;

     1 if [ $x == 0 ]
     2         then
     3             echo `date`>>${pn}_cpumem.txt
     4             echo "$pname:pid=$pid,maxcpu=$cpu,mincpu=$cpu,maxmem=$mem,minmem=$mem;">>${pn}_cpumem.txt        
     5             eval ${pn}_maxcpu=$cpu
     6             eval ${pn}_mincpu=$cpu
     7             eval ${pn}_maxmem=$mem
     8             eval ${pn}_minmem=$mem
     9             eval ${pn}_pid=$pid
    10         else
    11                         。。。
    12 fi

    4、比较原pid和当前pid,如果pid变化,进程可能崩溃重启过;取原有最大最小值,与当前值进行比较,其中,资源值因为是小数,不能使用-gt之类的进行比较,所以借用bc语言工具进行小数之间的比较;

     1        rpid=`eval echo '$'${pn}_pid`
     2             rmaxcpu=`eval echo '$'${pn}_maxcpu`
     3             rmincpu=`eval echo '$'${pn}_mincpu`
     4             rmaxmem=`eval echo '$'${pn}_maxmem`
     5             rminmem=`eval echo '$'${pn}_minmem`
     6             if [ $rpid != $pid ];then
     7                 eval ${pn}_pid=$pid
     8                 echo "$pname:pid changed,now pid=$pid">>${pn}_cpumem.txt        
     9             fi
    10             if [ `echo $cpu\>$rmaxcpu | bc` -eq 1 ];then
    11                 eval ${pn}_maxcpu=$cpu
    12                 echo "$pname:pid=$pid,maxcpu=$cpu;">>${pn}_cpumem.txt        
    13             elif [ `echo $cpu\<$rmincpu | bc` -eq 1 ];then
    14                 eval ${pn}_mincpu=$cpu
    15                 echo "$pname:pid=$pid,mincpu=$cpu;">>${pn}_cpumem.txt        
    16             fi
    17             if [ `echo $mem\>$rmaxmem | bc` -eq 1 ];then
    18                 eval ${pn}_maxmem=$mem
    19                 echo "$pname:pid=$pid,maxmem=$mem;">>${pn}_cpumem.txt        
    20             elif [ `echo $mem\<$rminmem | bc` -eq 1 ];then
    21                 eval ${pn}_minmem=$mem
    22                 echo "$pname:pid=$pid,minmem=$mem;">>${pn}_cpumem.txt        
    23             fi

    5、使用i值作为下标,数组下标从0开始,${#process[*]}取出的值是进程数组的长度;

    7     i=0
    8     while (($i<${#process[*]}))
    9     do
               。。。
    51       i=`expr $i+1`
    52    done

    6、隔十秒取一次资源值

    55     sleep 10

    7、最外层是使用一个无限循环不断取值。

    #调试脚本使用命令
    sh -x cpumem.sh
    #不中断的在后台运行
    setsid ./cpumem.sh &
    #查看脚本进程
    ps -ef |grep cpumem.sh

    8、需完善功能:①对于一个进程名查找出多个进程的情况,需要实现筛选机制;②输出结果仍然比较冗余,需要实现将所有进程最终最大值和最小值分别以一条记录的形式展现;记录每一次的pid的改变和改变时间;③需要实现指定脚本运行时间,不需要手动kill掉脚本进程;④系统关机后脚本需要能自启动。


    完善功能:指定脚本运行时间,不需要手动kill掉脚本,精简输出结果,将所有进程最终最大值和最小值分别以一条记录的形式展现,写入result_cpumem.txt中,第一次获取的资源值和每次pid的改变情况写入debug_cpumem.txt中

     1 #!/bin/bash
     2 process=(iss_server DbsServer.out)
     3 
     4 start=$(date +%s)
     5 take=0
     6 x=0
     7 while (( $take<20 ))
     8 do
     9         i=0
    10         while (($i<${#process[*]}))
    11         do
    12           pname=${process[$i]}
    14                 pn=`echo ${pname:0:3}|tr '[A-Z]' '[a-z]'`
    15 
    16                 pid=`ps -ef|grep $pname|grep -v grep|awk '{print $2}'`
    17                 cpu=`ps -p $pid -o pcpu --no-headers|sed "s/[[:space:]]//g"`
    18                 mem=`ps -p $pid -o pmem --no-headers|sed "s/[[:space:]]//g"`
    19                 if [ $x == 0 ]
    20                 then
    21                         echo `date`>>debug.txt
    22                         echo "$pname:pid=$pid,maxcpu=$cpu,mincpu=$cpu,maxmem=$mem,minmem=$mem;">>debug.txt
    23                         eval ${pn}_maxcpu=$cpu
    24                         eval ${pn}_mincpu=$cpu
    25                         eval ${pn}_maxmem=$mem
    26                         eval ${pn}_minmem=$mem
    27                         eval ${pn}_pid=$pid
    28                 else31                         rpid=`eval echo '$'${pn}_pid`
    32                         rmaxcpu=`eval echo '$'${pn}_maxcpu`
    33                         rmincpu=`eval echo '$'${pn}_mincpu`
    34                         rmaxmem=`eval echo '$'${pn}_maxmem`
    35                         rminmem=`eval echo '$'${pn}_minmem`
    36                         if [ $rpid != $pid ];then
    37                                 eval ${pn}_pid=$pid
    38                                 echo "$pname:pid changed,now pid=$pid">>debug.txt
    39                         fi
    40                         if [ `echo $cpu\>$rmaxcpu | bc` -eq 1 ];then
    41                                 eval ${pn}_maxcpu=$cpu
    42                         elif [ `echo $cpu\<$rmincpu | bc` -eq 1 ];then
    43                                 eval ${pn}_mincpu=$cpu
    44                         fi
    45                         if [ `echo $mem\>$rmaxmem | bc` -eq 1 ];then
    46                                 eval ${pn}_maxmem=$mem
    47                         elif [ `echo $mem\<$rminmem | bc` -eq 1 ];then
    48                                 eval ${pn}_minmem=$mem
    49                         fi
    50                 fi
    51                 i=`expr $i+1`
    52         done
    53         x=1
    54  sleep 10
    55         end=$(date +%s)
    56         take=$(( end - start ))
    57 done
    58 
    59 i=0
    60 echo `date`>>result_cpumem.txt
    61 while (($i<${#process[*]}))
    62 do
    63         pname=${process[$i]}
    64         pn=`echo ${pname:0:3}|tr '[A-Z]' '[a-z]'`
    65         maxcpu=`eval echo '$'${pn}_maxcpu`
    66         mincpu=`eval echo '$'${pn}_mincpu`
    67         maxmem=`eval echo '$'${pn}_maxmem`
    68         minmem=`eval echo '$'${pn}_minmem`
    69         echo "$pname:maxcpu=$maxcpu,mincpu=$mincpu,maxmem=$maxmem,minmem=$minmem;">>result_cpumem.txt
    70         i=`expr $i+1`
    71 done
  • 相关阅读:
    预处理
    关键字 static extern
    linux /proc/cpuinfo 不同处理器显示信息
    sscanf函数简介
    阿里云API接口调用
    AD常用命令以及概念
    开源工具、平台列表
    ELK安装部署
    MySQL8.0初体验
    MySQL案例09:Last_IO_Error: Got fatal error 1236 from master when reading data from binary log
  • 原文地址:https://www.cnblogs.com/cyanlee/p/11710752.html
Copyright © 2020-2023  润新知