nginx现在用yum源安装的话,都自带了logrotate
,默认是每天定时分割日志并压缩。
但是公司目前有一台生产环境的nginx不是用yum安装的。所以就得自己手动分割下日志了。
目前就是bash脚本+crontab实现,具体代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| #!/bin/bash
LOGS_PATH=/app/nginx/logs YESTERDAY=$(date -d "yesterday" +%Y%m%d)
ACCESS_FILE_NAME=${LOGS_PATH}/sxyz_erp_nginx_access_${YESTERDAY}
ERROR_FILE_NAME=${LOGS_PATH}/sxyz_erp_nginx_error_${YESTERDAY}
mv ${LOGS_PATH}/access.log ${ACCESS_FILE_NAME}.log tar -zcf ${ACCESS_FILE_NAME}.tar.gz ${ACCESS_FILE_NAME}.log rm -rf ${ACCESS_FILE_NAME}.log
mv ${LOGS_PATH}/error.log ${ERROR_FILE_NAME}.log tar -zcf ${ERROR_FILE_NAME}.tar.gz ${ERROR_FILE_NAME}.log rm -rf ${ERROR_FILE_NAME}.log
kill -USR1 $(cat /app/nginx/logs/nginx.pid)
cd ${LOGS_PATH} find . -mtime +20 -name "sxyz_erp_nginx_*" | xargs rm -f
exit 0
|