第一次遇到top查看swap时发现已使用的的swap有18446744073659357796kb这么多,使用以下脚本清理
for pid in `ls -l /proc|grep ^d |awk '{print $9}'|grep -v [^0-9]`
do
if [ $pid -eq 1 ]; then continue; fi
grep -q swap /proc/$pid/smaps 2>/dev/null
if [ $? -eq 0 ]; then
swap=$(grep swap /proc/$pid/smaps \
| gawk '{sum = $2;} end{print sum}')
porc_name=$(ps aux | grep -w "$pid" | grep -v grep \
| awk '{for(i = 11;i <= nf;i ){printf("%s ", $i)}}')
if [ $swap -gt 0 ];then
echo -e "${pid}\t${swap}\t${proc_name}"
fi
fi
done | sort -k2 -n | awk -f'\t' '{
pid[nr] = $1;
size[nr] = $2;
name[nr] = $3;
}
end{
for(id = 1; id <= length(pid); id )
{
if(size[id] < 1024)
1;
else if(size[id] < 1048576)
printf("%-10s\t.2fmb\t%s\n", pid[id], size[id]/1024, name[id]);
else
printf("%-10s\t.2fgb\t%s\n", pid[id], size[id]/1048576, name[id]);
}
}'