To view the script that the process occupies the swap partition
The command script is as follows:
The code is as follows |
Copy Code |
For i in ' Cd/proc;ls |grep "^[0-9]" |awk ' $ >100 ';d o awk '/swap:/{a=a+$2}end{print ' "$i" ', a/1024 "M"} '/proc/$i/smap S;d one |SORT-K2NR |
View the process that occupies swap
The code is as follows |
Copy Code |
#!/bin/bash
Echo-e ' Date +%y%m%d%h%m ' Echo-e "Pidttswapttproc_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 ("%-10st%15skbt%sn", Pid[id],size[id],name[id]); else if (size[id]<1048576) printf ("%-10st%15.2fmbt%sn", Pid[id],size[id]/1024,name[id]); Else printf ("%-10st%15.2fgbt%sn", Pid[id],size[id]/1048576,name[id]); } }' |
It is recommended to do timed tasks to monitor swap space usage
The code is as follows |
Copy Code |
Corntab-e 1 * * * * sh/root/swap.sh >>/root/swap/swap.log
|
There was a time when the machine's swap kept rising, after the monitoring found that some Java processes occupy swap space, completely do not release, kill these Java processes, release swap