docker 定时检查磁盘并清理

使用Docker容器部署镜像,可能需要定时检查磁盘空间。

  1. 编写 shell 脚本 threshold_script.sh
#!/bin/bash

LOG_FILE="/home/sh/threshold_script.log"

# 定义记录日志的函数
log() {
   
    local timestamp=$(date "+%Y-%m-%d %H:%M:%S")
    local log_message="[$timestamp] $1"
    echo "$log_message" >> "$LOG_FILE"
    echo "$log_message"
}

disk_space=$(df -h / | awk 'NR==2 {print $4}' | sed 's/G//')

threshold=50

if [ "$disk_space" -lt "$threshold" ]; then
    log "磁盘剩余空间${disk_space}G,小于 ${threshold}G,执行 docker system prune ..."
    docker system prune -f
    log "docker system prune 完成。"
else
    log "磁盘剩余空间${disk_space}G,大于等于 ${threshold}G,无需执行 docker system prune。"
fi
  1. 配置到系统定时任务中
crontab -e 
0 * * * * /bin/bash /home/sh/threshold_script.sh
  1. 重启定时任务
systemctl restart cron  # Ubuntu
systemctl restart crond # Centos

相关推荐

  1. docker 定时检查磁盘清理

    2023-12-15 19:00:04       59 阅读
  2. 如何清理Docker占用的磁盘空间?

    2023-12-15 19:00:04       46 阅读
  3. 解决docker中overlay2爆满,磁盘清理问题

    2023-12-15 19:00:04       54 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-15 19:00:04       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-15 19:00:04       106 阅读
  3. 在Django里面运行非项目文件

    2023-12-15 19:00:04       87 阅读
  4. Python语言-面向对象

    2023-12-15 19:00:04       96 阅读

热门阅读

  1. 爬虫心得分享小实用策略(应该不能算技巧)

    2023-12-15 19:00:04       59 阅读
  2. K8s client go 合并informer

    2023-12-15 19:00:04       60 阅读
  3. Scala-初学

    2023-12-15 19:00:04       62 阅读
  4. HackTheBox-Redeemer:Redis未授权访问

    2023-12-15 19:00:04       42 阅读
  5. SQL数列

    SQL数列

    2023-12-15 19:00:04      64 阅读
  6. Python实现单字母密码算法

    2023-12-15 19:00:04       62 阅读
  7. 12.使用 Redis 优化登陆模块

    2023-12-15 19:00:04       43 阅读
  8. python数据结构

    2023-12-15 19:00:04       57 阅读