nginx 日志切割改良版
shell脚本
vim cat_nginx_log.sh
#!/bin/bash #nginx日志文件的存放路径 logs_path='/app/openresty/nginx/logs/' ls -l ${logs_path}*.log| awk '{print $9}'|awk -v FS='/' '{print $6}' > ${logs_path}logname for i in `cat ${logs_path}logname`; do mv $logs_path${i} $logs_path${i}.$(date +%Y%m%d).log gzip $logs_path${i}.$(date +%Y%m%d).log done kill -USR1 `cat /app/openresty/nginx/logs/nginx.pid` /usr/bin/find ${log_path} -type f -name "*.gz" -mtime +30 -exec rm -f {} \;
crontab 定时任务
#每晚凌晨0点0分,对nginx日志做切割
0 0 * * * /bin/bash /app/openresty/nginx/logs/cat_nginx_log.sh