


















创建 memc 脚本:
cat > /usr/local/bin/memc << 'EOF' #!/bin/bash # 颜色定义 RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' CYAN='\033[0;36m' NC='\033[0m' # No Color # 清除屏幕 clear_screen() { printf "\033c" } # 显示当前内存状态 show_memory_status() { echo -e "${CYAN}========== 当前内存状态 ==========${NC}" free -h echo -e "${CYAN}==================================${NC}\n" } # 显示清理结果 show_result() { local choice=$1 echo -e "\n${GREEN}✓ 清理完成!${NC}" echo -e "${YELLOW}清理后的内存状态:${NC}" free -h } # 清理函数 clean_cache() { local option=$1 echo -e "${YELLOW}正在清理缓存...${NC}" # 执行sync确保数据写入磁盘 sync case $option in 1) echo -e "${BLUE}清理页面缓存 (Page Cache)...${NC}" echo 1 > /proc/sys/vm/drop_caches ;; 2) echo -e "${BLUE}清理目录项和索引节点 (dentries & inodes)...${NC}" echo 2 > /proc/sys/vm/drop_caches ;; 3) echo -e "${BLUE}清理全部缓存 (PageCache + dentries + inodes)...${NC}" echo 3 > /proc/sys/vm/drop_caches ;; esac show_result $option } # 显示菜单 - 修复版本 show_menu() { local selected=$1 shift local options=("$@") local i=0 echo -e "${CYAN}请选择要执行的清理操作:${NC}" echo -e "${CYAN}(使用上下箭头选择,回车确认)${NC}\n" for option in "${options[@]}"; do if [ $i -eq $selected ]; then echo -e "${GREEN}→ ${option}${NC}" else echo -e " ${option}" fi ((i++)) done } # 主函数 main() { # 检查是否为root用户 if [ "$EUID" -ne 0 ]; then echo -e "${RED}错误:请使用root权限运行此脚本${NC}" echo -e "${YELLOW}尝试:sudo $0${NC}" exit 1 fi # 检查drop_caches文件是否存在 if [ ! -f /proc/sys/vm/drop_caches ]; then echo -e "${RED}错误:系统不支持缓存清理${NC}" exit 1 fi # 菜单选项 - 修复:移除了多余的选项 options=( "1. 清理页面缓存 (Page Cache) - 最安全" "2. 清理目录项和索引节点 (dentries & inodes)" "3. 清理全部缓存 - 最彻底" "4. 退出" ) selected=0 key="" # 显示初始内存状态 clear_screen show_memory_status show_menu $selected "${options[@]}" # 处理键盘输入 while true; do read -rsn1 key if [[ $key == $'\x1b' ]]; then read -rsn2 key case $key in '[A') # 上箭头 if [ $selected -gt 0 ]; then ((selected--)) else selected=$((${#options[@]}-1)) # 循环到最后一个 fi clear_screen show_memory_status show_menu $selected "${options[@]}" ;; '[B') # 下箭头 if [ $selected -lt $((${#options[@]}-1)) ]; then ((selected++)) else selected=0 # 循环到第一个 fi clear_screen show_memory_status show_menu $selected "${options[@]}" ;; esac elif [[ $key == "" ]]; then # 回车键 case $selected in 0) # 选项1 clear_screen echo -e "${CYAN}========== 内存清理工具 ==========${NC}\n" show_memory_status echo -e "${YELLOW}您选择了:${options[0]}${NC}" read -p "确认执行清理操作?(y/n): " confirm if [[ $confirm == "y" || $confirm == "Y" ]]; then clean_cache 1 else echo -e "${BLUE}操作已取消${NC}" fi echo -e "\n${CYAN}按任意键返回主菜单...${NC}" read -n1 clear_screen show_memory_status show_menu $selected "${options[@]}" ;; 1) # 选项2 clear_screen echo -e "${CYAN}========== 内存清理工具 ==========${NC}\n" show_memory_status echo -e "${YELLOW}您选择了:${options[1]}${NC}" read -p "确认执行清理操作?(y/n): " confirm if [[ $confirm == "y" || $confirm == "Y" ]]; then clean_cache 2 else echo -e "${BLUE}操作已取消${NC}" fi echo -e "\n${CYAN}按任意键返回主菜单...${NC}" read -n1 clear_screen show_memory_status show_menu $selected "${options[@]}" ;; 2) # 选项3 clear_screen echo -e "${CYAN}========== 内存清理工具 ==========${NC}\n" show_memory_status echo -e "${YELLOW}您选择了:${options[2]}${NC}" read -p "确认执行清理操作?(y/n): " confirm if [[ $confirm == "y" || $confirm == "Y" ]]; then clean_cache 3 else echo -e "${BLUE}操作已取消${NC}" fi echo -e "\n${CYAN}按任意键返回主菜单...${NC}" read -n1 clear_screen show_memory_status show_menu $selected "${options[@]}" ;; 3) # 选项4 - 退出 echo -e "\n${GREEN}感谢使用,再见!${NC}" exit 0 ;; esac fi done } # 捕获Ctrl+C trap 'echo -e "\n${RED}程序被中断${NC}"; exit 1' INT # 运行主函数 main EOF
授权脚本:
chmod +x /usr/local/bin/memc
执行 memc 命令返回如下:
========== 当前内存状态 ========== total used free shared buff/cache available Mem: 31Gi 20Gi 9.9Gi 58Mi 1.2Gi 10Gi Swap: 8.0Gi 4.8Gi 3.2Gi ================================== 请选择要执行的清理操作: (使用上下箭头选择,回车确认) → 1. 清理页面缓存 (Page Cache) - 最安全 2. 清理目录项和索引节点 (dentries & inodes) 3. 清理全部缓存 - 最彻底 4. 退出
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。