Nginx日志切割脚本

 nginx  nginx   2016-11-05 

适用于以*.access.log开头的访问日志。

#!/bin/bash
#This script run at 00:00.
logs_path="/data/app/nginx/logs"
#切分后log存放的目录
bak_path="/data/logs"

yesterday=$(date -d "yesterday" +"%Y%m%d")
nginx_pid=$(cat ${logs_path}/nginx.pid)
host=$(ls -l ${logs_path}/*.access.log | awk -F " " '{print $9}' | awk -F ".access" '{print $1}' | awk -F "$logs_path/" '{print $2}')
for i in $host
do
  program=${bak_path}/$i
  bakfile=${program}/${yesterday}.tgz
  [ -d $program ] || mkdir -p $program
  if [ -f $bakfile ];then
    echo "已存在备份文件";exit 1
  else
    mv ${logs_path}/${i}.access.log $program;
    cd $program ; tar zcf $bakfile ${i}.access.log --remove
  fi
done
[ -d ${bak_path}/all ] || mkdir -p ${bak_path}/all
cd ${bak_path}/all
mv ${logs_path}/access.log . && tar zcf ${yesterday}.access.tgz access.log --remove
mv ${logs_path}/error.log . && tar zcf ${yesterday}.error.tar.gz error.log --remove

#向nginx主进程发信号重新打开日志
kill -USR1 $nginx_pid


点赞 打赏
取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

交流分享

QQ扫一扫或点击二维码加入,群号577543189
微信扫一扫,手滑关注它
微信扫一扫,进入小程序

推荐文章

标签云集

友情链接  交换友链

推广位