注意:此脚本可多用途,例如清理磁盘Disk_home=`df -m /home |sed '1d' |awk -F '%| +' ' {print $5}'`
当磁盘使用率大于%80的时候就删除相应的log日志
vim /home/scripts/free_clean.sh
#!/bin/bash
# 清理系统缓存脚本
# */5 * * * * / home/scripts /free_clean.sh >/dev/null 2>&1
used=`free -m |awk 'NR==2' |awk '{print $3}'`
free=`free -m |awk 'NR==2' |awk '{print $4}'`
echo "===========================" >>/var/log/mem.log
date +"%Y-%m-%d %H:%M.%S">> /var/log/mem.log
echo "Memory usage | [Use: ${used}MB][Free: ${free}MB]" >> /var/log/mem.log
if [ $free -le 500 ];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" >> /var/log/mem.log
else
echo "Not required" >> /var/log/mem.log
fi