采用Bash脚本性能监控过程

为一个Linux过程监控,采用Bash脚本。

采用ps命令的过程监控,使用周期加上连续监测的睡眠时间。

使用方法:

psmonitor.sh -p [pid] -d [interval] -n [statistics count]

參数:

-p 监控的进程ID

-d 读性能数据间隔

-n 统计次数,达到该次数。自己主动退出

#!/bin/bash
interval=0
count=0
pid=""
while getopts "p:d:n:" arg
do
        case $arg in
        p)
                pid=$OPTARG
                echo "pid: $pid"
                ;;
        d)
                interval=$OPTARG
                echo "interval:$interval"
                ;;
        n)
                count=$OPTARG
                echo "count:$count"
                ;;
        ?)
                echo "unkonw argument"
                exit 1
                ;;
        esac
done



i=0;

while [ true ]; do
        if [ $i -gt $count ]
        then
                exit 0;
        else
                let "i+=1"
        fi
         ps h -p $pid -o rss,vsz,%mem,%cpu
        sleep $interval

done

监控效果:

-bash-3.2$ ./psmonitor.sh -p 4181 -d 1 -n 10
pid: 4181
interval:1
count:10
1511232 2537664 37.3 2.4
1511232 2537664 37.3 2.4
1511232 2537664 37.3 2.4
1511232 2537664 37.3 2.4
1511232 2537664 37.3 2.4
1511232 2537664 37.3 2.4
1511232 2537664 37.3 2.4
1511232 2537664 37.3 2.4
1511232 2537664 37.3 2.4
1511232 2537664 37.3 2.4
1511232 2537664 37.3 2.4


版权声明:本文博主原创文章,博客,未经同意不得转载。

原文地址:https://www.cnblogs.com/blfshiye/p/4904093.html