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
相关推荐
数字新视界3 小时前
空调控制器AIRC800的智能化管理与应用实践
大数据·服务器·数据库·动环监控系统·动力与环境监控系统
知已难寻3 小时前
01 · OpenStack的认证管理 Keystone
运维·服务器·云原生·云计算·openstack
傅科摆 _ py3 小时前
DP,贪心,堆和栈,二分力扣复习
开发语言·python·算法
kiracrimson3 小时前
栈与队列容器:适配器、deque 与 priority_queue
开发语言·c++·适配器模式
北冥you鱼4 小时前
Go 语言 CRUD 实战:从接口设计到真实业务场景
开发语言·后端·golang
企鹅侠客4 小时前
Linux CPU 负载平均值(Load Average)详解:从原理到实践
linux·运维·服务器·服务器负载
郭小李4 小时前
springmvc 拦截器
java·开发语言·前端
web打印社区4 小时前
Chrome-kiosk-printing能上生产吗
开发语言·前端·javascript·chrome·websocket
吃一碗粉只付一碗钱4 小时前
10-令牌里的用户主键不要明文
服务器·数据库·sql·sqlserver·c#