View swap processes using shell scripts

Source: Internet
Author: User
#! /Bin/bashecho-e 'date + % y % m % d % H % M' echo-e & quot; PID \ t \ tSwap \ t \ tProc_Name & quot; # show all the directories named after numbers in the/proc directory (the process name is a number, and others such as sys and net store other information) forpidin 'ls-l/proc | grep ^ d | awk & #39 ;{
#! /Bin/bash echo-e 'date + % y % m % d % H % M' echo-e "PID \ t \ tSwap \ t \ tProc_Name" # output/proc directory all the directories named after numbers (the process name is a number, other information such as sys and net is stored) for pid in 'ls-l/proc | grep ^ d | awk '{print $9}' | grep-v [^ 0-9] 'Do # let the process release swap there is only one method: restart the process. Or wait until it is released automatically. Release # if the process is automatically released, we will not write the script to find him, because he is not automatically released. # 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 # restarting the init process means restarting the system, which is totally unavailable, so you don't have to detect him to avoid system impact. 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 fidone | 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]) ;}}'
We recommend that you perform scheduled tasks to monitor swap space usage. sh>/root/swap. after monitoring, some java processes occupy the swap space and are not released. after killing these java processes, swap is released.
Ps: http://blog.itpub.net/26972107/viewspace-774239
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.