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服务
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!