Increase timing Detection Linux memory, timely cleanup function
Original: http://www.voidcn.com/article/p-wnmannom-boa.html
Free-m view, found the memory ran full.
Top, and then press Shift+m, that is, by the percentage of memory occupied, found in the first process, only occupy 7.9%.
But the total memory size is 8G, even 5G is used,
What does that occupy?
Baidu a bit, is said to be centos in order to improve efficiency, the use of some of the files cached in memory.
If this is the case, I do not need this file performance, it can be released. The following two commands are available:
#sync
#echo 3 >/proc/sys/vm/drop_caches
After the memory is released, it takes up very little,
Write a script, and then use crontab timing no minutes to perform once, detect the amount of memory used more than 80% when the alarm, free the RAM,
Release the memory script as follows:
#!/bin/bash# System Allocated total area mem_total= ' free-m | awk ' nr==2 ' | awk ' {print $} ' #当前剩余的大小 mem_free= ' free-m | awk ' nr==3 ' | awk ' {print $4} ' #当前已使用的used大小 mem_used= ' free-m | grep Mem | awk ' {print $ } ' if (($mem _used! = 0)), then #如果已被使用 calculates the percentage of the total that is currently remaining free, expressed as a decimal number, to complement an integer bit before the decimal point 0 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# settings The alarm value is 20% (that is, when more than 80% is used). mem_warn=0.20 #当前剩余百分比与告警值进行比较 (returns 1 when it is greater than the alarm value (i.e. more than 20% remaining) and returns 0 if it is less than (that is, less than 20%) mem_now= ' expr $mem _per \> $mem _warn `
Let's talk about how to use timing:
I. Crontab INTRODUCTION
The function of the crontab command is to schedule execution of some commands at certain intervals.
Two View/etc/crontab File
Vim/etc/crontab
Adding timed tasks to crontab
Three Each line of the task in the file/etc/crontab is described in the following format:
Minute hour day Month DayOfWeek command
minute-integers from 0 to 59
hour-integers from 0 to 23
Day-an integer from 1 to 31 (must be a valid date for the specified month)
Month-an integer from 1 to 12 (or a month such as the Jan or Feb abbreviation)
DayOfWeek-integers from 0 to 7, 0 or 7 to describe Sunday (or as represented by sun or mon shorthand)
Command-commands to execute (commands that can be used as Ls/proc >>/tmp/proc or execute custom scripts)
If it is performed every five minutes, it can be changed to:
*/5 * * * */home/dbbackup/db2backup.sh
Four Startup shutdown for Crontab services
Sbin/service crond Start//start service/sbin/service Crond stop//shutdown service/sbin/service crond restart//Restart service/sbin/service Crond rel Oad//Reload Configuration
The code is as follows:
#!/bin/bashecho "Current time is $ (date-d" Today "+"%y-%m-%d-%h-%m-%s ")" >>/usr/local/tomcat/logs/monitor/mem_ auto_$ (date +%y%m%d). log# System Allocated total area mem_total= ' free | grep "Mem:" |awk ' {print $} ' echo ' Mem_total is $mem _total ">>/usr/local/tomcat/logs/monitor/mem_auto_$ (date +%y %m%d). log# Current remaining size mem_free= ' free | grep ' Buffers/cache ' | awk ' {print $} ' echo ' Mem_free is $mem _free ">>/usr/local/tomcat/logs/monitor/mem_auto_$ (date +%y%m%d). log# Used size currently in use mem_used= ' free-m | grep Mem | awk ' {print $} ' echo ' mem_used is $mem _used ">>/usr/local/tomcat/logs/monitor/mem_auto_$ (date +%y%m%d). Logif (( $mem _used! = 0)); Then #如果已被使用, the percentage of the total that is currently remaining free is calculated, expressed in decimal notation, to complement an integer digit 0 mem_per=0 ' echo ' scale=2 before the decimal point; $mem _free/$mem _total "| BC ' echo ' free percent is $mem _per ">>/usr/local/tomcat/logs/monitor/mem_auto_$ (date +%y%m%d). Logdata=" $ (date-d "Today" + "%y-%m-%d-%h-%m-%s") Free percent is: $mem _per "Echo $DATA >>/usr/local/tomcat/logs/monitor/mem_auto_$ ( Date +%y%m%d)The. Logecho $DATA >>/var/log/mem_detect.log# Set the alarm value to 44% (i.e. alarm when using more than 56%). mem_warn=0.4 echo "Mem_warn is $mem _warn" >>/usr/local/tomcat/logs/monitor/mem_auto_$ (date +%y%m%d). log# The current remaining percentage is compared to the alarm value (1 is returned when it is greater than the alarm value (that is, more than 44% remaining), and 0 is returned when it is less than (i.e., the remaining less than 44%) mem_now= ' expr $mem _per \> $mem _warn ' echo ' remaining percentage compared to the warning value Mem_now is $mem _now ">>/usr/local/tomcat/logs/monitor/mem_auto_$ (date +%y%m%d). Logecho" When Mem_now was 1, means M EM is OK! ">>/usr/local/tomcat/logs/monitor/mem_auto_$ (date +%y%m%d). Logecho"-----------------------------------" >>/usr/local/tomcat/logs/monitor/mem_auto_$ (date +%y%m%d). log# If the current use is more than 56% (that is, the remainder is less than 44%, the above return value equals 0), Free the memory if (($ Mem_now = = 0)); Then echo the Mem_now is 0, less than (i.e. less than 44% of the memory left), so clean up the memory, start to clear memery ... ">>/usr/local/tomcat/logs/mon itor/mem_auto_$ (date +%y%m%d). Logsyncecho 1 >/proc/sys/vm/drop_cachesecho 2 >/proc/sys/vm/drop_cachesecho 3 >/proc/sys/vm/drop_cachesecho "---> Start auto clear memery is OK! $DATA, warnIs $mem _warn, less than (that is, memory remaining less than 44%), so clean up memory, ">>/usr/local/tomcat/logs/monitor/mem_auto_$ (date +%y%m%d_%h). logfi# Take the current idle CPU percent ratio (take only the integer part) cpu_idle= ' Top-b-N 1 | grep Cpu | awk ' {print $} ' | Cut-f 1-d "." ' Echo ' cpu_idle percent is $cpu _idle,cpu the remaining amount is sufficient, the warning value is when the remaining less than 30%, automatically clean up inside ">>/usr/local/tomcat/logs/monitor/mem_auto_ $ (date +%y%m%d). Logecho "================================================================" >>/usr/local/ tomcat/logs/monitor/mem_auto_$ (date +%y%m%d). log# set the idle CPU alarm value to 30%, if the current CPU uses more than 70% (that is, the remaining less than 30%), immediately send an email alert, automatically clean up memory if (($ Cpu_idle< 30)); thenecho " CPU left less than 30%, so clean up memory, start to clear memery ..." >>/usr/local/tomcat/ logs/monitor/mem_auto_$ (date +%y%m%d). log Syncecho 1 >/proc/sys/vm/drop_cachesecho 2 >/proc/sys/vm/drop_ Cachesecho 3 >/proc/sys/vm/drop_cachesecho "--->cpu used more than 70%, so start auto clear Memery is OK! $DATA, warn is $mem _warn ">>/usr/local/tomcat_treasure/logs/memery_monitor/memstat_cpu_auto_$ (date +%Y%m%d_%H ). Logfifi
(turn) Increase timing detection Linux memory, timely cleanup function