如何在 ubuntu 中安排 cron 作业以在存储内存达到 90% 满后删除文件

原文标题how to schedule a cron job in ubuntu to delete files after storage memory get 90% full

我想在 Linux 中安排一个具有以下条件的 cron 作业:

  1. 如果 90% 已满,则应检查存储内存空间–>
  2. 应先删除旧文件,如 FIFO(Queue),直到内存达到 10%
  3. 并返回可用内存

原文链接:https://stackoverflow.com//questions/71465900/how-to-schedule-a-cron-job-in-ubuntu-to-delete-files-after-storage-memory-get-90

回复

我来回复
  • Stev的头像
    Stev 评论

    尽管在我看来,您将能够自己进行这项研究。一些建议:

    1. 检查磁盘空间
      进口舒蒂尔
      总计,已使用,空闲 = shutil.disk_usage(“/”)print(“Total: %d GiB” % (total // (2**30)))
      print(“已使用:%d GiB” % (已使用 // (2**30)))
      print(“免费:%d GiB” % (免费 // (2**30)))
    2. 列出特定目录中的文件并按日期排序
      导入操作系统
      search_dir = “/tmp/del”
      os.chdir(search_dir)
      文件 = 过滤器(os.path.isfile,os.listdir(search_dir))
      files = [os.path.join(search_dir, f) for f in files] # 添加每个文件的路径
      files.sort(key=lambda x: os.path.getmtime(x))
      打印(文件)
    3. 删除文件
      导入操作系统
      os.remove(file_location)
    2年前 0条评论