#!/bin/sh #日志查找路径 logFolders=("/home/workspace/logs/zpc_mall_payment"); zipLogTime=`date -d "$(date +%Y%m)01 last month" +%Y-%m`; #查找2天前的日志 for logFolder in ${logFolders[*]} do echo "跳转到${logFolder}目录"; cd ${logFolder}; echo "当前目录为:`pwd`"; n=`find ${logFolder} -name "*$(date -d "$(date +%Y%m)01 last month" +%Y-%m)*.log" | wc -l`; if [ ${n} -gt 0 ];then echo "当前日志文件夹下需压缩日志数量:${n},暂对上个月的日志文件进行压缩"; echo "开始压缩日志......" tar -zcvf ${zipLogTime}.tar.gz `find ./* -name "*$(date -d "$(date +%Y%m)01 last month" +%Y-%m)*.log"`; if [ $? -eq 0 ];then echo "日志文件压缩成功!删除原日志文件......" for log in $(find ./* -name "*$(date -d "$(date +%Y%m)01 last month" +%Y-%m)*.log") do rm -f ${log}; if [ $? -eq 0 ];then echo `date`"${log}日志文件删除成功!" else echo `date`"${log}日志文件删除失败!" fi done else echo "日志文件压缩失败!" fi else echo "当前日志文件夹下需压缩日志数量:0" fi done 查看现有的crontab 任务 crontab -l 创建新的crontab任务 crontab -e 30 4 1 * * sh /home/workspace/shellDir/zipPaymentLog.sh >> /data/logs/deletelog.log
标签:log,第一天,压缩,echo,+%,上个月,date,日志 From: https://www.cnblogs.com/zhangdapangzo/p/17210831.html