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服务

作者:集君

出处:https://www.cnblogs.com/chq3272991/p/18498898

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

posted @   集君  阅读(15)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
more_horiz
keyboard_arrow_up dark_mode palette
选择主题
menu
点击右上角即可分享
微信分享提示