#!/bin/bash
echo -e ‘date +%y%m%d%H%M’
echo -e “PID\t\tSwap\t\tProc_Name”
# Show all in the name of digital in/proc directory directories (the process name is the only process that other store other information such as sys,net)
for pid in ‘ls -l /proc | grep ^d | awk ‘{ print $9 }’| grep -v [^0-9]’
do
# Allow the process to release the swap method is only one: is to restart the process. Or release it automatically. Put
# If the process automatically releases, then we wouldn’t have written a script to look for him, find him is because he is not automatically released.
# Listed so we have to take up swap and need to restart the process, but the init process of this process is the ancestor of all processes in the system
# Restart the init process means a system reboot, this was unthinkable, so you don’t have to test him, so as not to affect the system.
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 }’)
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
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)
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]);
}
}'
Recommended timing task to monitor swap space usage
corntab -e
1 * * * * sh /root/swap.sh >> /root/swap/swap.log
Once upon a time machine swap won’t stop rising, monitor and found some Java processes use swap space, completely free, after killing the Java process, release the swap.
Leave a Reply