服务器没有开swap,所以cache的东西多了会死机,而且重启无效,很头疼
写个代码来定时清理cache吧
#!/bin/bash total=`free -m | awk 'NR==2' | awk '{print $2}'` used=`free -m | awk 'NR==2' | awk '{print $3}'` free=`free -m | awk 'NR==2' | awk '{print $4}'` top=80 echo "===========================" >> /var/log/mem.log date >> /var/log/mem.log echo "Memory usage | [Use:${used}MB][Free:${free}MB]" >> /var/log/mem.log need_clear=`echo "$used $total $top" | awk '{if(($1/$2)*100>$3) {printf "%d", 1} else {printf "%d", 0}}'` if [ $need_clear = 1 ] ; then percent_used=`echo "$used $total" | awk '{printf "%.2f", ($1/$2)*100}'` echo "Used by percent is $percent_used" >> /var/log/mem.log 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 "Sync and Clear" >> /var/log/mem.log else echo "Not required" >> /var/log/mem.log fi
做个计划任务
nano -w /usr/lib/systemd/system/freemem.timer
[Unit] Description=free the cache in memory of archlinx daily [Timer] OnCalendar=*-*-* *:50:00 AccuracySec=4h Persistent=true [Install] WantedBy=timers.target
修改后重新加载服务
systemctl daemon-reload
nano -w /usr/lib/systemd/system/freemem.service
[Unit] Description=free the cache in memory of archlinx daily [Service] Nice=19 IOSchedulingClass=2 IOSchedulingPriority=7 ExecStart=/srv/jobs/freemem.sh
设置为开机启动
systemctl enable freemm.timer