#!/bin/bash echo -e "PID\t\tSwap\t\tProc_Name" for pid in `ls -l /proc | grep ^d | awk '{ print $9 }'| egrep -v [a-z]+` do #进程id是1为祖进程 if [ $pid -eq 1 ];then continue;fi # Do not check init process # 判断改进程是否占用了swap grep -q "Swap" /proc/$pid/smaps 2>/dev/null if [ $? -eq 0 ];then # 如果占用了swap swap=$(grep Swap /proc/$pid/smaps | gawk '{ sum+=$2;} END{ print sum }') proc_name=$(ps aux | grep -w "$pid" | grep -v grep | awk '{ for(i=11;i<=NF;i++){ printf("%s ",$i); }}') if [ $swap -gt 0 ];then # 如果占用了swap则输出其信息 echo -e "$pid\t${swap}\t$proc_name" fi fi done | sort -k2 -n | gawk -F'\t' '{ # 按占用swap的大小排序,再用awk实现单位转换。 # 如:将1024KB转换成1M。将1048576KB转换成1G,以提高可读性。 pid[NR]=$1; size[NR]=$2; name[NR]=$3; } END{ for(id=1;id<=length(pid);id++) { if(size[id]<1024) printf("%-10s\t%15sKB\t%s\n",pid[id],size[id],name[id]); else if(size[id]<1048576) printf("%-10s\t%15.2fMB\t%s\n",pid[id],size[id]/1024,name[id]); else printf("%-10s\t%15.2fGB\t%s\n",pid[id],size[id]/1048576,name[id]); } }'
转载于:https://blog.51cto.com/kkkkkk/1629574