脚本内容:
cat /app/cut_ngx_log.sh
#!/bin/sh
# Program:
# Auto cut nginx log script.
# nginx日志路径
LOGS_PATH=/app/nginx/logs
TODAY=$(date -d 'today' +%Y-%m-%d)
# 移动日志并改名
mv ${LOGS_PATH}/error.log ${LOGS_PATH}/error_${TODAY}.log
mv ${LOGS_PATH}/access.log ${LOGS_PATH}/access_${TODAY}.log
# 向nginx主进程发送重新打开日志文件的信号
kill -USR1 $(cat /app/nginx/logs/nginx.pid)
#自动删除30天前的指定日志文件
find ${LOGS_PATH}/ -mtime +30 -name "error_*.log" -exec rm -rf {} \;
find ${LOGS_PATH}/ -mtime +30 -name "access_*.log" -exec rm -rf {} \;
授权并添加定时任务
chmod +x /app/cut_ngx_log.sh
echo '00 00 * * * sh /app/cut_ngx_log.sh >> /app/nginx/logs/cutnginxlog.log 2>&1' >> /etc/crontab