存档

文章标签 ‘内存’

ubuntu自动执行crontab释放内存(free-m)

2015年5月18日 没有评论

写了ubunt下的shell脚本,用来自动监听内存使用情况,当可用内存小于指定大小的时候,自动释放内存。

#!/bin/bash
ROOT_UID=0
E_NOTROOT=67
if [ “$UID” -ne “$ROOT_UID” ]
then
echo “Must be root to run this script”
exit $E_NOTROT
fi

free -m |grep -i mem |awk ‘{if($4 > 1000){ printf(“good”) > “free_m_temp.txt”}else {printf(“bad”) > “free_m_temp.txt”}}’

#read line one
line=`awk “{ if (NR == 1) print}” “free_m_temp.txt”`

if [ “$line” == “bad” ]
then
sh -c “sync; echo 3 > /proc/sys/vm/drop_caches”
#service apache2 restart
#service mysql restart
echo “done , free memory with drop caches”
else
echo “no need to free memory”
fi

 

方案2:

#!/bin/bash
ROOT_UID=0
E_NOTROOT=67
if [ “$UID” -ne “$ROOT_UID” ]
then
echo “Must be root to run this script”
exit $E_NOTROT
fi

timenow=$(date +%M)
free -m |grep -i mem |awk ‘{printf($4-0) > “/home/vanpeople/free_m/freesize.txt”}’
cp -r /home/vanpeople/free_m/freesize.txt /home/vanpeople/free_m/log_size/$timenow.txt

line=`awk “{ if (NR == 1) print}” “/home/vanpeople/free_m/freesize.txt”`

if [ $line -lt 3000 ]
then
sh -c “sync; echo 3 > /proc/sys/vm/drop_caches”
timenow_ymdhis=$(date +%Y-%m-%d.%H:%M:%S)
echo $timenow_ymdhis.$line > /home/vanpeople/free_m/log_drop/$timenow_ymdhis.txt
#service apache2 restart
#service mysql restart
echo “done , free memory with drop caches”
else
echo “no need to free memory”
fi

 

分类: ubuntu高手 标签: , , ,
css.php