How to list the processes that are taking up swap
This article will explain in detail how to list the processes that are occupying swap. The content of the article is of high quality. Therefore, Xiaobian shares it with you as a reference. I hope that after reading this article, you will have a certain understanding of relevant knowledge.
List the processes that are consuming swap
#!/ bin/bash
###############################################################################
#Date: October 31, 2011
#Author: xiaoxi227
# Email : xiaoxi227@163.com
# QQ : 543928910
#Version: 1.0
#Script function: List the processes that are consuming swap.
#Call relationship:
#Other Notes:
###############################################################################
echo -e "PID\t\tSwap\t\tProc_Name"
#Take out all directories named by numbers under/proc directory (process name is number is process, others such as sys,net are other information)
for pid in `ls -l /proc | grep ^d | awk '{ print $9 }'| grep -v [^0-9]`
do
#There is only one way for a process to release swap: restart the process. Or wait for it to release itself.
#If the process is automatically released, then we won't write scripts to find it, we will find it because it is not automatically released.
#So we want to list the processes that take up swap and need to be restarted, but init is the ancestor of all processes in the system
#Restarting the init process means restarting the system, which is absolutely not allowed, so there is no need to detect him, so as not to affect the system.
if [ $pid -eq 1 ];then continue;fi # Do not check init process
#Determine whether the change process occupies swap
grep -q "Swap" /proc/$pid/smaps 2>/dev/null
if [ $? -eq 0 ];then #if swap is occupied
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