delete file shell 让 AI 写脚本

写一个shell /app使用超过80% 删除100个最久的 /app/pge17/backups/archive下的00000开头的文件

执行时加上日期

执行时加上清理的文件名字

复制代码
#!/bin/bash

# Define the monitored mount point and the target backup directory
TARGET_DIR="/app/pge17/backups/archive"
THRESHOLD=80

# Function to print log messages with the current timestamp
log_msg() {
    echo "$(date '+[%Y-%m-%d %H:%M:%S]') $1"
}

# 1. Get the current disk usage percentage of the /app partition (extract numbers only)
USAGE=$(df -h | awk '$NF=="/app"{sub(/%/,"",$5); print $5}')

# If matching by mount point fails, fallback to path-based matching
if [ -z "$USAGE" ]; then
    USAGE=$(df -h "$TARGET_DIR" | awk 'NR==2{sub(/%/,"",$5); print $5}')
fi

# 2. Check if the disk usage exceeds the threshold
if [ "$USAGE" -gt "$THRESHOLD" ]; then
    log_msg "WARNING: /app disk usage has reached ${USAGE}%, exceeding the threshold of ${THRESHOLD}%!"
    
    # 3. Verify if the target directory exists
    if [ -d "$TARGET_DIR" ]; then
        log_msg "Scanning for the 100 oldest files starting with '00000' in ${TARGET_DIR}..."
        
        # Get the list of the top 100 oldest files and store them in a temporary variable
        # Using IFS= read -r to handle files properly
        FILE_LIST=$(find "$TARGET_DIR" -maxdepth 1 -type f -name "00000*" -printf '%T+ %p\n' 2>/dev/null | sort | head -n 100 | cut -d' ' -f2-)

        # Check if any matching files were found
        if [ -n "$FILE_LIST" ]; then
            log_msg "The following files will be deleted:"
            
            # Print each file name with a timestamp
            echo "$FILE_LIST" | while read -r file; do
                log_msg "Removing file: $file"
            done
            
            # Perform the actual deletion
            echo "$FILE_LIST" | xargs -r rm -f
            log_msg "Cleanup completed successfully."
        else
            log_msg "No files starting with '00000' were found to delete."
        fi
    else
        log_msg "ERROR: Target directory ${TARGET_DIR} does not exist!"
    fi
else
    log_msg "/app disk usage is normal (${USAGE}%), no cleanup needed."
fi

*/5 * * * * /app/scripts/delarch.sh >>/app/scripts/delarch.log 2>&1

/app/scripts/delarch.log 被删除后执行不了如何处理

为了防止以后日志文件再次被误删导致任务失败,你可以直接在 delarch.sh脚本的最开头( #!/bin/bash**下方)**加上这行代码:

bash

复制代码
# Auto-create the log file if it was deleted
touch "/app/scripts/delarch.log" 2>/dev/null
相关推荐
feixing_fx38 分钟前
JavaScript 是什么:从浏览器脚本到通用编程语言
开发语言·前端·javascript·前端框架·ecmascript
无足鸟ICT2 小时前
【RHCA+】sosreport(收集诊断文档)
linux·运维·服务器
惜.己2 小时前
Java结合idea简单使用Playwright
java·开发语言·自动化
利威尔xu3 小时前
PostgreSQL 索引失效 4 类:函数运算让查询全表扫描拖垮性能
开发语言·数据库·postgresql·性能优化·索引优化
ShineWinsu3 小时前
对于Qt:QSpacerItem常见属性以及方法的解析
开发语言·c++·qt·面试·系统架构·客户端·qspaceritem
瑞雪兆丰年兮5 小时前
Linux零基础快速入门-第二章 Linux基础命令
linux·运维·服务器
ShineWinsu6 小时前
对于Qt:QGripLayout常见属性、方法以及QSizePolicy的解析
开发语言·c++·qt·面试·gui·客户端·网格
艾莉丝努力练剑6 小时前
【AI大模型接入SDK】C++的SessionManager 线程安全
java·开发语言·前端·c++·人工智能·学习·安全
利威尔xu7 小时前
Go Mutex 饥饿模式:正常模式等待者饿死,1ms 阈值自动切换崩溃
开发语言·后端·面试·golang·并发编程
光电笑映8 小时前
TCP 为什么可靠?从三次握手、滑动窗口到拥塞控制全面拆解(上)
运维·服务器·网络·udp