登录服务器之后,编写shell定时任务脚本freemem.sh
vim freemem.sh
写入以下文件内容:
#! /bin/sh
used=`free -m | awk 'NR==2' | awk '{print $3}'`
free=`free -m | awk 'NR==2' | awk '{print $4}'`
echo "===========================" >> /tmp/logs/mem.log
date >> /tmp/logs/mem.log
echo "Memory usage before | [Use:${used}MB][Free:${free}MB]" >> /tmp/logs/mem.log
if [ $free -le 6000 ] ; 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]" >> /tmp/logs/mem.log
echo "OK" >> /tmp/logs/mem.log
else
echo "Not required" >> /tmp/logs/mem.log
fi
exit 1
授权
chmod u+x /root/freemem.sh
使用crontab -e命令编辑当前用户的crontab,30分钟执行一次
*/30 * * * * /root/freemem.sh
重启crond服务
/sbin/service crond restart
查看crond服务是否重启成功
/sbin/service crond status