借鉴于apacherizhi/ target=_blank class=infotextkey>apache日志分割的bash脚本,而nginx的日志分割和apache的分割操作过程基本相同。
例子,nginx日志的分割与压缩脚本。
复制代码 代码示例:
#!/bin/bash
# This script run at 00:00
# www.jb200.com
# The Nginx logs path
logs_path="/usr/local/nginx/logs/"
cd $logs_path
for logfile in `ls *.log`
do
mv ${logs_path}${logfile} ${logs_path}$(date -d "yesterday" +"%Y%m%d_")${logfile}
done
kill -USR1 `cat /usr/local/nginx/nginx.pid`
tar czvf $(date -d "yesterday" +"%Y%m%d")_log.tar.gz $(date -d "yesterday" +"%Y%m%d_")*.log # 打包压缩日志
rm -f $(date -d "yesterday" +"%Y%m%d_")*.log # 删除压缩源文件