监控脚本——检测进程内存变化

#!/bin/bash
self_pid=$$
echo $self_pid
count=`ps axu |grep -E ${1}|grep -E ${0} |grep -v grep |grep -v ${self_pid}|wc -l`
echo $count
if [ $count -ne 1 ]
then
    echo $(ps aux|grep -E ${1}|grep -E ${0}|grep -v grep|grep -v ${self_pid})
    ps axu |grep -E $1 |grep -E $0 |grep -v grep |grep -v $self_pid |awk '{print $2}'|xargs kill -9
fi
res=record_$1_res.csv
virt=record_$1_virt.csv
echo $res
echo $virt
>$res
>$virt
for ((;;))
do  
    top -n 1 b|grep $1|grep -v logfork  |awk '{print $5}'|sed 's#[a-z]##g' >>$virt
    top -n 1 b|grep $1|grep -v logfork  |awk '{print $6}'|sed 's#[a-z]##g' >>$res
    sleep 3
done

sh record.sh bin

 

#!/bin/bash
PROCESS=进程名
LOG="/mnt/memlog.txt"
sleep 10
#删除上次的监控文件
if [ -f "$LOG" ];then
rm "$LOG"
fi
#过滤出需要的进程ID
PID=$(ps | grep $PROCESS | grep -v 'grep' | awk '{print $1;}')
while [ "$PID" != "" ]
do
cat /proc/$PID/status | grep RSS >> "$LOG"#过滤出VmRSS行
sleep 5
PID=$(ps | grep $PROCESS | grep -v 'grep' | awk '{print $1;}')
done

 

posted @ 2019-05-20 17:36  鸭子船长  阅读(817)  评论(0编辑  收藏  举报