背景为公司项目为前后端分离项目,前端用nginx部署,本身服务器(linux)不太好,其次nginx日志产生过多,导致产生过多的垃圾,经过一番百度,已经有很多前辈,帮助解决了问题.如下:

1 编写一个脚本 名字任意 xxx.sh
内容为

#!/bin/bash
find /usr/local/nginx/logs/ -mtime +15 -type f -name *.log | xargs rm -f

放到服务器的任意位置,我是放到了nginx 文件夹中
解读一下 +15 删除15天之前的日志,可以再此更改时间
2 编写定时任务

[root@localhost ~]# crontab -e

插入内容为 :

00 01 * * * /bin/bash /root/shell/autodellogs.sh

解读一下 00 01 每天凌晨一点执行定时任务 /bin/bash 执行脚本 后边为脚本位置以及脚本名称对应(xxx.sh)

3 重启定时任务,令其生效

[root@localhost ~]# service crond restart

4 如果报错,一般为需要用systemctl ,那么执行如下命令:

systemctl restart crond.servi

至此,已经完成了自动删除功能