Linux内存释放脚本

By | 2013年2月17日

创建下面这个文件freemem.sh,我放在/opt/freemem目录下。

#!/bin/sh
Time1=`date +%Y%m%d`
Time2=`date +%k:%M:%S`
cd /var/log/freemem
#
#echo "">memstat_$Time1.log
#
echo $Time1--$Time2 >>memstat_$Time1.log
echo "------------------" >>memstat_$Time1.log
echo "The mem stat is :" >>memstat_$Time1.log
echo "" >>memstat_$Time1.log
free -m >>memstat_$Time1.log
echo "" >>memstat_$Time1.log
#sync
echo "....Freeing......" >>memstat_$Time1.log
echo "">>memstat_$Time1.log
#
echo 1 > /proc/sys/vm/drop_caches
echo 2 > /proc/sys/vm/drop_caches
echo 3 > /proc/sys/vm/drop_caches
echo "---> OK ! " >>memstat_$Time1.log
echo "">>memstat_$Time1.log
free -m >>memstat_$Time1.log
echo "=========================================================================">> memstat_$Time1.log
echo "">>memstat_$Time1.log
echo "">>memstat_$Time1.log
cd -
cat /var/log/freemem/memstat_$Time1.log

给予执行权限

chmod 755 /opt/freemem/freemem.sh

创建日志目录

mkdir /var/log/freemem

定时执行freemem.sh

以crontab方式:

vim /etc/crontab
#每隔6小时执行一次
*  */6  *  *  *  root  /opt/freemem/freemem.sh

发表评论

电子邮件地址不会被公开。 必填项已用*标注