惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Simon Willison's Weblog
Simon Willison's Weblog
Help Net Security
Help Net Security
P
Privacy International News Feed
T
Threat Research - Cisco Blogs
C
Cisco Blogs
C
CERT Recently Published Vulnerability Notes
NISL@THU
NISL@THU
L
LINUX DO - 热门话题
Security Latest
Security Latest
A
Arctic Wolf
G
GRAHAM CLULEY
月光博客
月光博客
S
Securelist
D
Docker
J
Java Code Geeks
T
Troy Hunt's Blog
T
Tenable Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
SecWiki News
SecWiki News
S
Security @ Cisco Blogs
量子位
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
L
LINUX DO - 最新话题
Recent Commits to openclaw:main
Recent Commits to openclaw:main
aimingoo的专栏
aimingoo的专栏
博客园 - 【当耐特】
H
Heimdal Security Blog
The Hacker News
The Hacker News
博客园 - 三生石上(FineUI控件)
Application and Cybersecurity Blog
Application and Cybersecurity Blog
N
Netflix TechBlog - Medium
Vercel News
Vercel News
Forbes - Security
Forbes - Security
B
Blog RSS Feed
H
Hackread – Cybersecurity News, Data Breaches, AI and More
IT之家
IT之家
B
Blog
MongoDB | Blog
MongoDB | Blog
博客园 - 聂微东
Google DeepMind News
Google DeepMind News
S
Secure Thoughts
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Check Point Blog
云风的 BLOG
云风的 BLOG
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
T
The Blog of Author Tim Ferriss
L
Lohrmann on Cybersecurity
F
Full Disclosure
D
Darknet – Hacking Tools, Hacker News & Cyber Security
P
Proofpoint News Feed

博客园 - Nihaorz

告别闪烁,拥抱流畅:在 Windows Terminal 中完美配置 Cygwin 环境 Nginx 透明代理 + 自动回源存储:访问即缓存,落盘即文件 创建 docker ipvlan,让 docke 容器获取独立ip 电犀牛 R68s iStoreOS 2.5G 网口速率优化 解决 openwrt ssh 命令行终端 home、end 键不可用问题 一键添加视频封面脚本 ffmpeg 转码参数 docker save 远程 ssh 主机直接 load,不产生本地文件 AutoHotKey 脚本 - win10 自动连接无线显示器 SSH 登录/退出实时监控脚本 OpenClaw 安装部署,配置 deepseek curl 断点续传下载 debian iso 镜像下载地址 linux 安装 zerotier,加入网络 基于 Fail2ban 的 SSH 入侵自动反制方案 ssh 配置密钥登录,关闭密码登录 基于 Fail2ban 的 OpenWRT SSH 入侵自动反制方案 Linux Screen 命令速查 使用 ofelia 在 docker 容器中执行计划任务 linux 磁盘挂载示例
memc - 基于 shell 的交互式清理内存脚本
Nihaorz · 2026-03-04 · via 博客园 - Nihaorz

创建 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. 退出

posted @ 2026-03-04 10:57  Nihaorz  阅读(20)  评论()    收藏  举报