linux定时释放缓存,定时释放Linux/CentOS缓存

定时释放Linux/CentOS缓存的脚本(yl_dropcaches)如下:

#!/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

#!/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 +xyl_dropcaches)后,将其加入定时任务中:

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)如下:

# 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

# 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

从左至右,每个*分别代表:分,时,日,月,周;最后为要执行的命令;

其中* / - , 这些通配符的含义为:

*  表示所有数值,如第一位使用* 表示每分钟

/  表示每,如第一位使用 */5 表示每5分钟

-  表示数值范围,如第二位使用2-4表示2点到4点

,  表示离散的多个数值,如第2位使用6,8 表示6点和8点

指定"步长":8-14/2 表示8,10,12,14

指定列表:比如 "1,2,3,4″,"0-4,8-12″

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值