- 在目录/home/web/ 下新建packlog.sh
#!/bin/bash#author:#date:#department:echo `date '+%Y-%m-%d %H:%M:%S'`" 打包程序执行开始!";file_path=/testeight_days_ago=`date -d '8 days ago' +%Y-%m-%d`yesterday=`date -d 'yesterday' +%Y-%m-%d`echo $file_path,$eight_days_ago,$yesterdayif [ -d $file_path ] then#压缩前一天日志cd $file_path &&cp -rf catalina.out ${yesterday}_catalina.out\&&tar -czf ${yesterday}_catalina.out.tar.gz ${yesterday}_catalina.out\#删除8天前的日志rm -rf catalina.out.$eight_days_ago.tar.gz\#删除新生成的outrm -rf ${yesterday}_catalina.out\#清空catalina.out:cat /dev/null > catalina.out elseecho "tomcat logs directory is not exist" fiecho '备份结束'
- 使用以下命令修改文本格式否则会报Syntax error: "fi" unexpected (expecting "then")
sed -i 's/\r//' /home/web/packlog.sh
- /bin/sh packlog.sh 执行验证 看程序是否正常执行
- 使用命令crontab -e 添加定时任务 列出以下命令选择编码文件 本人选择 2
Select an editor. To change later, run 'select-editor'.1. /bin/nano <---- easiest2. /usr/bin/vim.basic3. /usr/bin/vim.tinyChoose 1-3 [1]:
- 每天凌晨1点打包前一天日志,并清理一周前日志压缩包
0 1 * * * /bin/sh /home/web/packlog.sh