bash shell检测进程内存变化

2019-01-22  本文已影响0人  zigbee0809

#!/bin/sh

get_mem()

{

process_id=$1

text=`cat /proc/${process_id}/status | grep VmRSS`

#没有这个进程

if [ "${text}" = "" ] ; then

memory=0

echo ${memory}

return 0

fi

memory=`echo $text | tr -cd "[0-9]"`

echo ${memory}

return 0

}

PROCESS_NAME="a.out"

pid=`pidof ${PROCESS_NAME}`

echo pid:${pid}

#没有这个进程

if [ "${pid}" = "0" ]; then

max_mamory=0

else

max_memory=$(get_mem ${pid})

fi

echo pid=${pid},max_mem=${max_memory}

while [ true ] ; do

sleep 1s

#得到进程号

pid=`pidof ${PROCESS_NAME}`

if [ "${pid}" = "0" ];then

# 没找到复位

max_memory=0

continue

fi

#得到进程使用的内存

current_memory=$(get_mem ${pid})

if [ "${current_memory}" = "0" ]; then

continue

fi

#如果占用内存增加了,输出

if [ ${current_memory} -ne ${max_memory} ]; then

echo

echo ------------------

date

diff=`expr ${current_memory} - ${max_memory}`

echo ${current_memory} - ${max_memory} = ${diff}

max_memory=${current_memory}

fi

done

上一篇 下一篇

猜你喜欢

热点阅读