Loading

linux硬盘清理

日志文件清理

#!/bin/bash

# 定义需要检查的目录
directories=("/home/logs/archived" \
             "/home/logs/archived" \
             "/data/logs" \
             "/data/logs")

# 获取当前硬盘分区的使用情况
df_output=$(df --output=pcent /data | tail -n 1 | tr -d ' %')

# 获取当前时间
current_time=$(date "+%Y-%m-%d %H:%M:%S")

# 判断硬盘使用是否超过70%
if [ "$df_output" -gt 70 ]; then
    echo "$current_time: Disk usage is above 70%. Cleaning up logs..."
    for dir in "${directories[@]}"; do
        # 检查目录是否存在
        if [ -d "$dir" ]; then
            # 进入目录
            pushd "$dir" > /dev/null || continue
            # 删除除了最新的5个文件之外的所有文件
            find . -type f -printf '%T+ %p\n' | sort -r | tail -n +6 | cut -d' ' -f2- | xargs rm -f
            echo "$dir is cleanup"
            # 返回上级目录
            popd > /dev/null
        else
            echo "$current_time: Directory$dir does not exist, skipping."
        fi
    done
    echo "$current_time: Log cleanup complete."
else
    echo "$current_time: Disk usage is below 70%. No cleanup needed."
fi

定时任务,每小时的第30分钟执行一次

30 * * * * /home/script/clean-td-prod-log.sh >> /home/script/logfile.log 2>&1

docker容器日志限制

修改/etc/docker/daemon.json ,添加如下内容:

{
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "100m",
    "max-file": "3"
  }
}

重启docker服务

posted @ 2024-10-24 09:46  集君  阅读(3)  评论(0编辑  收藏  举报