In Linux, view the process script that occupies swap 01 #! /Bin/bash02 03 ################################### ######################################## ### 04 # script functions: list the processes that are occupying swap. 05 ####################################### ######################################## 06 07 echo-e "PID \ t \ tSwap \ t \ tProc_Name" 08 09 # show all the directories named by numbers in the/proc directory (process name is a number, other information such as sys and net is stored) 10for pid in 'LS-l/proc | grep ^ d | awk '{print $9}' | grep-v [^ 0-9] '11do12 # Let the process release swap there is only one method: restart the process. Or wait until it is released automatically. Put 13 # If the process will be automatically released, then we will not write the script to find him, because he is not automatically released. 14 # So we need to list the processes that occupy swap and need to be restarted, but the init process is the ancestor process of all processes in the system. 15 # restarting the init process means restarting the system, so you don't have to detect him to avoid system impact. 16 if [$ pid-eq 1]; then continue; fi17 grep-q "Swap"/proc/$ pid/smaps 2>/dev/null18 if [$? -Eq 0]; then19 swap =$ (grep Swap/proc/$ pid/smaps \ 20 | gawk '{sum + = $2;} END {print sum }') 21 proc_name = $ (ps aux | grep-w "$ pid" | grep-v grep \ 22 | awk '{for (I = 11; I <= NF; I ++) {printf ("% s", $ I) ;}} ') 23 if [$ swap-gt 0]; then24 echo-e "$ {pid} \ t $ {swap} \ t $ {proc_name}" 25 fi 26 fi 27 done | sort-k2-n | awk-F '\ t' {28 pid [NR] = $1; 29 size [NR] = $2; 30 name [NR] = $3; 31} 32END {33 for (id = 1; id <= length (pid ); id ++) 34 {35 if (size [id] <1024) 36 printf ("%-10s \ t % 15sKB \ t % s \ n", pid [id], size [id], name [id]); 37 else if (size [id] <1048576) 38 printf ("%-10s \ t % 15.2fMB \ t % s \ n", pid [id], size [id]/1024, name [id]); 39 else40 printf ("%-10s \ t % 15.2fGB \ t % s \ n", pid [id], size [id]/1048576, name [id]); 41} 42 }'