nginx 按天日志分割 for linux

Shell脚本代码:

#!/bin/bash

nginx_pid=/var/run/nginx.pid

nginx_log_dir=/data/logs/nginx

goaccess_dir=/opt/goaccess

#yyyy

year=`date -d "yesterday" +"%Y"`

#MM

month=`date -d "yesterday" +"%m"`

#yyyyMMdd

day=`date -d "yesterday" +"%Y%m%d"`

daycount=30

dayold=`date -d "$daycount day ago" +"%Y%m%d"`

nginx_log_old=$nginx_log_dir/access.log.$dayold

if [ -f "$nginx_log_old" ]; then 

  rm  $nginx_log_old

fi 

cd $nginx_log_dir

if [ ! -f "access.log.$day" ]; then 

 mv access.log access.log.$day

 kill -USR1 `cat $nginx_pid`

fi

cd $goaccess_dir

mkdir -p html/$year/$month

bin/goaccess -f $nginx_log_dir/access.log.$day -d -a -p etc/goaccess.conf > html/$year/$month/$day.html

相关推荐