The shell script implements monitoring swap space usage and viewing processes that occupy swap _linux shell

Source: Internet
Author: User

Copy Code code as follows:

#!/bin/bash

Echo-e ' Date +%y%m%d%h%m '
Echo-e "Pid\t\tswap\t\tproc_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 ("%-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]);
}
}

It is recommended to do timed tasks to monitor swap space usage

Copy Code code as follows:

Corntab-e
1 * * * * sh/root/swap.sh >>/root/swap/swap.log

There was a time when the machine's swap kept rising, and after monitoring it was found that some of the Java processes occupy swap space, completely do not release, kill these Java processes, release swap.

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.