#!/bin/bash LIMIT=512 LOG_FILE="/data/logs/timing_dropcaches.log" #定时清理系统内存 #https://blog.csdn.net/gaojinshan/article/details/40710369 used=`free -m | awk 'NR==2' | awk '{print $3}'` free=`free -m | awk 'NR==2' | awk '{print $4}'` echo "===========================" >> $LOG_FILE date +"%Y-%m-%d %H:%M.%S" >> $LOG_FILE echo "Memory usage | [Use:${used}MB][Free:${free}MB] | limit[${LIMIT}MB]" >> $LOG_FILE # drop caches when the free memory less than 512M if [ $free -le $LIMIT ] ; 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" >> $LOG_FILE else echo "Not required" >> $LOG_FILE fi
以上脚本加入crontab
#每半小时检查内存使用情况,并清理一次内存
*/30 * * * * /data/shell/timing_dropcaches.sh >> /data/logs/timing_dropcaches.log
#删除3天前的日志
0 1 * * * /usr/bin/find /data/logs/ -name *.log -mtime +3 | grep 2018 | xargs rm -Rf {} > /dev/null 2>&1