最近有个服务总不定时CPU飚满~已知是内存溢出导致,你等着抓它的时候,它又正常了。索性写个脚本抓它,如下:
#!/bin/bash
# 加载java环境
source /etc/profile
# pid是我提前获取的,就要抓它
monitor_pid=31665
countfile=/XXX/temp.count
dumpfile=/XXX/temp.hprof
if [ ! -f ${dumpfile} ]; then
cpu_usage=`top -bn1 | grep ${monitor_pid} | awk '{print int($9)}'`
# cpu使用率根据自己需求改一下
if [ ${cpu_usage} -gt 700 ]; then
echo ${cpu_usage} >> ${countfile}
if [ `cat ${countfile} | wc -l` -ge 6 ];then
jmap -dump:live,format=b,file=${dumpfile} ${monitor_pid}
fi
else
echo '' > ${countfile}
fi
fi
一分钟跑一次吧~
*/1 * * * * bash /XXX/temp20210317.sh