定時釋放Linux/CentOS緩存

定時釋放Linux/CentOS緩存的腳本(yl_dropcaches)以下:  python

  1. #!/bin/bash  
  2. used=`free -m | awk 'NR==2' | awk '{print $3}'`  
  3. free=`free -m | awk 'NR==2' | awk '{print $4}'`  
  4.   
  5. echo "===========================" >> yl_dropcaches.log  
  6. date >> yl_dropcaches.log  
  7. echo "Memory usage | [Use:${used}MB][Free:${free}MB]" >>  yl_dropcaches.log  
  8.   
  9. # drop caches when the free memory less than 10G  
  10. if [ $free -le 10000 ] ; then  
  11.  #sync && echo 1 > /proc/sys/vm/drop_caches  
  12.  #sync && echo 2 > /proc/sys/vm/drop_caches  
  13.  sync && echo 3 > /proc/sys/vm/drop_caches  
  14.  echo "OK" >>  yl_dropcaches.log  
  15. else  
  16.  echo "Not required" >> yl_dropcaches.log  
  17. fi  
#!/bin/bash
used=`free -m | awk 'NR==2' | awk '{print $3}'`
free=`free -m | awk 'NR==2' | awk '{print $4}'`

echo "===========================" >> yl_dropcaches.log
date >> yl_dropcaches.log
echo "Memory usage | [Use:${used}MB][Free:${free}MB]" >>  yl_dropcaches.log

# drop caches when the free memory less than 10G
if [ $free -le 10000 ] ; 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
 echo "OK" >>  yl_dropcaches.log
else
 echo "Not required" >> yl_dropcaches.log
fi

改成可執行(chmod +x yl_dropcaches)後,將其加入定時任務中:緩存

  1. echo '*/30 * * * * sh /home/yl_dropcaches' >> /var/spool/cron/root  
echo '*/30 * * * * sh /home/yl_dropcaches' >> /var/spool/cron/root

而後,重啓一下便可(service crond restart)。
CentOS默認會將crontab設置爲開啓的狀態; 可查看其狀態或開啓:service crond status/start/stop/restart/reload  能夠用如下命令來操做crontab: crontab -l #查看List   crontab -e #編輯Edit crontab -r #刪除Remove crontab的規則(cat /etc/crontab)以下:
bash

  1. # Example of job definition:  
  2. # .---------------- minute (0 - 59)  
  3. # |  .------------- hour (0 - 23)  
  4. # |  |  .---------- day of month (1 - 31)  
  5. # |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...  
  6. # |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat  
  7. # |  |  |  |  |  
  8. # *  *  *  *  * user-name command to be executed  
# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed

從左至右,每一個*分別表明:分,時,日,月,周;最後爲要執行的命令;less

 

其中* / - , 這些通配符的含義爲:ui

  1. *  表示全部數值,如第一位使用* 表示每分鐘    
  2. /  表示每,如第一位使用 */5 表示每5分鐘    
  3. -  表示數值範圍,如第二位使用2-4表示2點到4點    
  4. ,  表示離散的多個數值,如第2位使用6,8 表示6點和8點    
  5. 指定"步長":8-14/2 表示8,10,12,14    
  6. 指定列表:好比 "1,2,3,4″,"0-4,8-12″   
相關文章
相關標籤/搜索