#!/bin/bash
ps -e -o 'rsz,pid,' | sort -nrk1 | head -5 > /tmp/np.txt
ps -e -o 'pcpu,pid' | sort -nrk1 | head -5 > /tmp/up.txt
#查询内存使用情况
echo "========================================================================================================="
echo "==========================="
echo "内存占用排名前五位的应用为:"
echo "==========================="
echo -e '\n'
cat /tmp/np.txt | while read NLINE
do
echo "应用为:"
ps -ef |grep `echo $NLINE | awk '{print $2}'` | grep -v "grep"
echo "占用内存为(G):"
NPN1=`echo $NLINE | awk '{print $1}'`
NPN2=1048576
awk 'BEGIN{printf "%.2f\n",'$NPN1'/'$NPN2'}'
echo -e '\n'
done
echo "========================================================================================================="
#查询CPU使用率
echo -e '\n'
echo "========================================================================================================="
echo "============================"
echo "CPU使用率排名前五位的应用为:"
echo "============================"
cat /tmp/up.txt | while read ULINE
do
echo "应用为:"
ps -ef |grep `echo $ULINE | awk '{print $2}'` | grep -v "grep"
echo "cup使用率为:"
echo $ULINE | awk '{print $1}'
echo -e '\n'
done
echo "========================================================================================================="
#查询内存和CPU使用率都较高的应用
echo -e '\n'
echo "========================================================================================================="
echo "============================="
echo "内存和CPU使用率都较高的应用:"
echo "============================="
echo -e '\n'
cat /tmp/np.txt | while read NULINE
do
if [ "$(find /tmp/up.txt | xargs grep `echo $NULINE | awk '{print $2}'`)" != "" ];then
echo "应用为:"
ps -ef |grep `echo $NULINE | awk '{print $2}'` | grep -v "grep"
echo -e '\n'
echo "占用内存为(G):"
NPN1=`echo $NULINE | awk '{print $1}'`
NPN2=1048576
awk 'BEGIN{printf "%.2f\n",'$NPN1'/'$NPN2'}'
echo -e '\n'
echo "cup使用率为:"
find /tmp/up.txt| xargs grep `echo $NULINE | awk '{print $2}'`| awk '{print $1}'
echo -e '\n'
fi
done
echo "========================================================================================================="
转载于:https://blog.51cto.com/lrtao2010/1861450