Use shell script to detect memory usage in Linux

Source: Internet
Author: User
Tags memory usage

The company server has been dying for the last two days because of the full memory usage. Really can't stand the old call room to help people to restart, specifically wrote a script to release the memory, of course, this server is not too important, my rude way of handling estimates will not be suitable for most of the server, please use before you think clearly, Otherwise, it would be none of my business to cause data loss.
Script content:


Cat check-mem.sh
#!/bin/bash
Mem_total= ' Free-m | awk ' nr==2 ' | awk ' {print $} '
Mem_free= ' Free-m | awk ' nr==3 ' | awk ' {print $} '
Mem_used= ' Free-m | grep Mem | awk ' {print $} '
if (($mem _used!= 0)); Then
Mem_per=0 ' echo ' scale=2 $mem _free/$mem _total "| BC '
Data= "$ (date-d" Today "+"%y-%m-%d-%h-%m ") free percent are: $mem _per"
Echo $DATA >>/var/log/mem_detect.log
mem_warn=0.20
mem_now= ' expr $mem _per\> $mem _warn '
if (($mem _now = 0)); Then
Sync
Echo 3 >/proc/sys/vm/drop_caches
Fi
Fi


Here's a description:

#!/bin/bash

#系统分配的区总量
Mem_total= ' Free-m | awk ' nr==2 ' | awk ' {print $} '

#当前剩余的大小
Mem_free= ' Free-m | awk ' nr==3 ' | awk ' {print $} '

#当前已使用的used大小
Mem_used= ' Free-m | grep Mem | awk ' {print $} '

if (($mem _used!= 0)); Then

#如果已被使用, calculates the percentage of the total amount currently remaining free, expressed as a decimal number, and complements an integer digit 0 before the decimal point.


Mem_per=0 ' echo ' scale=2 $mem _free/$mem _total "| BC '
Data= "$ (date-d" Today "+"%y-%m-%d-%h-%m ") free percent are: $mem _per"
echo $DATA >>/var/log/mem_detect.log


#设置的告警值为20% (that is, when using more than 80% alarm).


mem_warn=0.20

#当前剩余百分比与告警值进行比较 (1 is returned when it is greater than the alarm value (i.e., more than 20%), and 0 is returned when less than (i.e., less than 20% remaining)


mem_now= ' expr $mem _per\> $mem _warn '

#如果当前使用超过80% (that is, the remaining is less than 20%, the return value above equals 0), freeing memory


if (($mem _now = 0)); Then
Sync
Echo 3 >/proc/sys/vm/drop_caches
Fi
Fi

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.