View swap process scripts in Linux

Source: Internet
Author: User
In Linux, view the process script that occupies swap 01 #! /Bin/bash0203 #################################### ######################################## #04 # Script functions: list the processes that are occupying swap. 05 ################### Linux view swap process Script 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 }'
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.