`

(测试可用)Centos中定时清理内存

 
阅读更多

第一步:编写 shell 文件:dropcache.sh

 

#!/bin/bash
used=`free -m | awk 'NR==2' | awk '{print $3}'`
free=`free -m | awk 'NR==2' | awk '{print $4}'`

echo "===========================" >> /opt/dropcache/logs.txt
date >> /opt/dropcache/logs.txt
echo "Memory usage | [Use:${used}MB][Free:${free}MB]" >>  /opt/dropcache/logs.txt

# drop caches when the free memory less than 10G
if [ $free -le 10000 ] ; then
 #sync && echo 1 > /proc/sys/vm/drop_caches
 #sync && echo 2 > /proc/sys/vm/drop_caches
 sync && echo 3 > /proc/sys/vm/drop_caches
 echo "OK" >>  /opt/dropcache/logs.txt
else
 echo "Not required" >> /opt/dropcache/logs.txt
fi

 

shell 主要是列出当前使用了多少内存,如果小于10G,则将内存清理掉,并将日志写入 logs.txt 的文件中。

如果超过10G也写入日志文件,但是会提示: Not required 表示无需清理。

 

第二步:在 crontab 的定时任务中加入该任务的执行,每天凌晨4点钟执行

 

0 4 * * * sh /opt/dropcache/dropcache.sh

 

日志内容格式如下:

===========================

Mon Mar 18 17:40:25 CST 2019

Memory usage | [Use:3982MB][Free:969MB]

OK

===========================

Tue Mar 19 04:00:01 CST 2019

Memory usage | [Use:4740MB][Free:11211MB]

Not required

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics