1 準備一個定時釋放內存的命令:freemem.shvim
# vim freemem.sh #!/bin/bash used=`free -m | awk 'NR==2' | awk '{print $3}'` free=`free -m | awk 'NR==2' | awk '{print $4}'` filename=/var/log/mem.log filesize=`ls -l $filename | awk '{ print $5 }'` maxsize=$((1024*2)) if [ $filesize -gt $maxsize ] then sudo rm $filename else echo "" fi echo "===========================" >> /var/log/mem.log date >> /var/log/mem.log echo "Memory usage | [Use:${used}MB][Free:${free}MB]" >> /var/log/mem.log if [ $free -le 100 ] ; then sync && echo 1 > /proc/sys/vm/drop_cach 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
2 將執行任務寫入 定時任務bash
sudo crontab -e 結尾添加: */1 * * * * sudo sh /home/dxs/software/freemem.sh ## 意思是:設置每1min執行一次,會自動寫入以前腳本里指定的log
3 從新啓動定時任務ui
重啓服務: sudo service cron restart ============================= 修改rsyslog sudo vim /etc/rsyslog.d/50-default.conf cron.* /var/log/cron.log #將cron前面的註釋符去掉 重啓rsyslog sudo service rsyslog restart sudo service cron restart 查看crontab日誌 tail -f /var/log/cron.log
4 完成,之後不再會死機了。。。。。。rest