[运维|shell] 编写shell脚本定期清理日志

以下是一个简单的示例,它将删除指定目录下30天内未被修改的日志文件:

#!/bin/bash
log_path="/home/server/core/logs/app"
if [ -d "${log_path}" ]; then
  echo "start delete log 30 days ago..."
  find ${log_path} -mtime +30 -type f -delete
  echo "done."
fi

将其保存为一个名为logAutoClean.sh文件,然后在终端中运行以下命令以使其可执行:

chmod +x logAutoClean.sh

接下来,您可以使用crontab定时器来定期运行此脚本。例如,要在每天凌晨1点运行此脚本,请在终端中输入以下命令:

crontab -e

然后添加以下行:

0 1 * * * /path/to/logAutoClean.sh

相关推荐

  1. [|shell] 编写shell脚本定期清理

    2023-12-25 23:20:01       38 阅读
  2. 定时清理Linux服务器缓存shell脚本

    2023-12-25 23:20:01       12 阅读
  3. Golang - 执行shell脚本,实时输出shell脚本中的

    2023-12-25 23:20:01       36 阅读
  4. 【280个shell脚本】----提示工作效率

    2023-12-25 23:20:01       8 阅读
  5. Linux SHELL脚本编程

    2023-12-25 23:20:01       22 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-25 23:20:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-25 23:20:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-25 23:20:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-25 23:20:01       20 阅读

热门阅读

  1. 第6章1-字符串及正则表达式 p63

    2023-12-25 23:20:01       32 阅读
  2. 避免约束系数过大的2种技巧

    2023-12-25 23:20:01       36 阅读
  3. ubuntu22.04上安装charles-proxy

    2023-12-25 23:20:01       34 阅读
  4. GO语言基础笔记(四):并发编程基础

    2023-12-25 23:20:01       33 阅读
  5. 第六章 卷:将磁盘挂载到容器

    2023-12-25 23:20:01       33 阅读
  6. Android 设置系统桌面壁纸

    2023-12-25 23:20:01       35 阅读
  7. 【C++PCL】点云处理最小三维圆拟合

    2023-12-25 23:20:01       40 阅读