开启debian定时任务crond,每天凌晨检查一次free内存,当小于2G时执行这三条命令(注:系统内容8G)。
注意:在执行这三条命令之前一定要先执行sync命令(描述:sync 命令运行 sync 子例程。如果必须停止系统,则运行sync 命令以确保文件系统的完整性。sync 命令将所有未写的系统缓冲区写到磁盘中,包含已修改的 i-Node、已延迟的块 I/O 和读写映射文件)
debian启动脚本: clean-buff-cache.sh
#! /bin/sh used=`free -m | awk 'NR==2' | awk '{print $3}'` free=`free -m | awk 'NR==2' | awk '{print $4}'` echo "===========================" >> /tnwmall/memory/logs/mem.log date >> /tnwmall/memory/logs/mem.log echo "Memory usage before | [Use:${used}MB][Free:${free}MB]" >> /tnwmall/memory/logs/mem.log if [ $free -le 2000 ] ; 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 used_ok=`free -m | awk 'NR==2' | awk '{print $3}'` free_ok=`free -m | awk 'NR==2' | awk '{print $4}'` echo "Memory usage after | [Use:${used_ok}MB][Free:${free_ok}MB]" >> /tnwmall/memory/logs/mem.log echo "OKAY" >> /tnwmall/memory/logs/mem-okay.log else echo "Not required" >> /tnwmall/memory/logs/mem-required.log fi exit 1