1、创建文件并填入下来内容:
vi clear_log.shell
#!/bin/bash
##获取上个月的月份
lastMonth=`date -d "last month" +%Y%m`
----压缩当前目录下的日志文件并删除日志文件
tar -zcvf ${lastMonth}.tar.gz ${lastMonth}*.log --remove-files
#查找当前目录下前两个月的压缩文件并删除
find ./ -mtime +60 -name "*.tar.gz" -exec rm -f {} \;
2、给文件赋予执行权限
chmod +x clear_log.shell
3、设置自动任务,每个月1号执行一遍脚本
crontab -e
0 0 1 * * /root/clear_log.shell
tips:如果crontab时间格式不对,保存该自动任务之后,会有这个提示
Do you want to retry the same edit?
crontab命令格式 crontab A B C D E 命令
A:分钟
B:小时
C:日
D:月
E:周