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
Copy Code code as follows:
#!/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:
Copy Code code as follows:
#!/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