Copy Code code as follows:
#!/bin/bash
Echo-e ' Date +%y%m%d%h%m '
Echo-e "Pid\t\tswap\t\tproc_name"
# Take out all directories in the/proc directory (the process name is the number is the process, others, such as sys,net, etc. store other information)
For PID in ' Ls-l/proc | grep ^d | awk ' {print $} ' | grep-v [^0-9] '
Todo
# There is only one way to let a process release swap: To restart the process. Or wait for it to be released automatically. Put
# If the process is automatically released, then we won't write a script to find him, because he is not automatically released.
# so we're going to list the processes that occupy swap and need to reboot, but the init process is the ancestor process of all processes in the system
# Restarting the INIT process means focusing on the system, which is absolutely not possible, so you don't have to test him to avoid the impact on 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]);
}
}
It is recommended to do timed tasks to monitor swap space usage
Copy Code code as follows:
Corntab-e
1 * * * * sh/root/swap.sh >>/root/swap/swap.log
There was a time when the machine's swap kept rising, and after monitoring it was found that some of the Java processes occupy swap space, completely do not release, kill these Java processes, release swap.