监控生产服务器内存使用前十

#!/usr/bin/env bash 
#date 20170726 
#destination: 
st_file="/root/list.txt"
log_file="/root/mem_monitor.txt"
log_report="/root/java_mem_use.txt"
###########################top 在centos7.2 中,不能使用-a 选项,则先更改top的配置,再保存,则能很好处理脚本问题;
top -a -b -n 1 > ${st_file}
#sleep 1 
pid_10=$(head -n 17 ${st_file} | tail -n 10 | awk '{print $1}')
mem_10=$(head -n 17 ${st_file} | tail -n 10 | awk '{print $10}')
pid_array=(${pid_10})
mem_array=(${mem_10})
echo "$(date +%F-%H:%m:%S)>>>>>>>>>>>>>>>>>>>>>>>>>>>" >>${log_report}
for index in $(seq 10)
do
    program=$(ps -elf | grep ${pid_array[(($index-1))]} | grep -v grep | awk '{print $15,$16,$17'})
    #echo "-------------PID-----------------------"
    echo "占用内存第${index}的PID:   ${pid_array[(($index-1))]} " >> ${log_report}
    echo "此PID的程序为:  ${program} " >> ${log_report}
    echo "此进程占用的内存为:   ${mem_array[(($index-1))]} " >>${log_report}
    echo "=======================================" >>${log_report}
done
原文地址:https://www.cnblogs.com/Mail-maomao/p/7241634.html