#!/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
网友评论