#!/bin/bashnginx
#Nginx的log日誌目錄centos
log_path='/opt/app/nginx/logs/'bash
#Nginx的pid文件存放目錄app
pid_path="${log_path}nginx.pid"ide
#清理掉指定日期前的日誌centos7
DAYS=60spa
#生成昨天的日誌文件日誌
#若指定10天前 date -d"10 day ago 2017-06-08" +%Y-%m-%dit
mv ${log_path}access.log ${log_path}access_$(date -d "yesterday" +"%Y%m%d").logclass
mv ${log_path}error.log ${log_path}error_$(date -d "yesterday" +"%Y%m%d").log
kill -USR1 `cat ${pid_path}`
#文件夾大小轉換成M
#文件大小默認是字節
size=`du -b ${log_path} | awk '{print int($1/1024/1024)}'`
#刪除60天前的文件
if [ "$size" -gt "1000" ];then
#find 對應目錄 -mtime +天數 -type f -name "文件名" -exec rm -rf {} \;
find ${log_path} -mtime +$DAYS -type f -name "*.log" -exec rm -rf {} \;
fi
#下面是任務計劃
[root@centos7 ~]# cat /var/spool/cron/root
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
export PATH
0 0 * * * /bin/sh /opt/log_backup.sh
#賦予權限
chmod +x /opt/log_backup.sh