Skip to content

Instantly share code, notes, and snippets.

@AGou-ops
Created January 31, 2024 07:02
Show Gist options
  • Save AGou-ops/293038ecd67c47aa6fb68bab99876b54 to your computer and use it in GitHub Desktop.
Save AGou-ops/293038ecd67c47aa6fb68bab99876b54 to your computer and use it in GitHub Desktop.
#!/bin/bash
# 获取总内存大小
total_mem=$(free -m | awk 'NR==2{print $2}')
# 获取buff/cache大小
buff_cache=$(free -m | awk 'NR==2{print $6}')
# 计算buff/cache占总内存的百分比
usage_percentage=$((buff_cache * 100 / total_mem))
echo $total_mem $buff_cache $usage_percentage
# 判断是否超过60%
if [ $usage_percentage -gt 60 ]; then
echo "Buff/Cache usage is above 60%. Cleaning buff/cache..."
# 执行清理buff/cache的命令,例如使用sync和echo 3 > /proc/sys/vm/drop_caches
sync && echo 3 > /proc/sys/vm/drop_caches
echo "Buff/Cache cleaned."
else
echo "Buff/Cache usage is below 60%. No action needed."
fi
@AGou-ops
Copy link
Author

crontab:

*/30 * * * * /root/clean_buff_cache.sh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment