시간 에 따라 Nginx 로 그 를 압축 하고 n 전의 로 그 를 청소 합 니 다.

6274 단어 shell
Nginx 로 그 는 매일 정시 에 n 전의 로 그 를 압축 하고 청소 합 니 다.
다음은 셸 스 크 립 트 입 니 다. 스 크 립 트 실행 도 실행 로 그 를 만 들 고 경 로 는 자동 으로 변 경 됩 니 다.스 크 립 트 는 주로 지정 한 로그 디 렉 터 리 의 모든 *. log 파일 을 날짜 별로 압축 하고 Nginx 신 호 량 제어 로 그 를 다시 읽 습 니 다 (kill - USR 1 cat **/nginx.pid. 마지막 으로 n 일 전의 로그 압축 파일 을 삭제 합 니 다.
crond 로 정시 작업 추가
##            
crontab -e
##       1     
0 1 * * * sh (    )/cutNginxLog.sh
#!/bin/bash
##function: cut nginx log files

## set the log path for this shell Script
cutNginxLog_path="/applog/cutNginxLog"
cutNginxLog_log="/applog/cutNginxLog/cutNginxLog.log"

## set nginx log path and nginx pid path
nginx_log_path="/applog/nginx"
nginx_log_pid="/applog/nginx/nginx.pid"
nginx_log_dir=${nginx_log_path}

## set how long you want to save
save_days=7


###############################################
## Please do not modify the following Script ##
###############################################

## create the log path for this Script
### -x
if [ ! -x "$cutNginxLog_path" ];then
  mkdir "$cutNginxLog_path"
fi
### -d
if [ ! -d "$cutNginxLog_path" ];then
  mkdir "$cutNginxLog_path"
fi
### -f
if [ ! -d "$cutNginxLog_log" ];then
  touch "$cutNginxLog_log"
fi

echo "-------------------------------------------------------------" >> $cutNginxLog_log
echo "-------- start cut the log files $(date +"%Y-%m-%d %H:%M:%S") --------" >> $cutNginxLog_log
echo "-------------------------------------------------------------" >> $cutNginxLog_log

## print the number of day need to save
echo "save days is $save_days" >> $cutNginxLog_log

## get yesterday and print
YESTERDAY=$(date -d "yesterday" +"%Y-%m-%d")
echo "yesterday is $YESTERDAY" >> $cutNginxLog_log

### function: get the nginx log files you want to cut ###
function fGetLogFilesName()
{
    log_path=$1;
    log_fullFileNames=$(ls ${log_path}*.log);
    c=0;
    for log_fullFileName in $log_fullFileNames
    do
        log_fullFileName_tmp=${log_fullFileName##*/};
        log_filename=${log_fullFileName_tmp%.*};
        log_name[$c]="$log_filename";
        ((c++));
    done
    echo ${log_name[*]};
}
#########################################################

### function: print the array of log files name ###
function fPrintLogFilesName()
{
    log_name_arr=$1;
    for log_value in ${log_name_arr[*]}
    do
        echo "fileName: $log_value" >> $cutNginxLog_log
    done
}
###################################################

### function: cut nginx log files ###
#parameters:[1]arraySize,[2]logPath,[3]logNameArray
function fCutLog()
{
    log_num=$1;
    log_path=$2;
    log_name_arr=($3);
    for((i=0;i> $cutNginxLog_log
        mv ${log_path}${log_name_arr[i]}.log ${log_path}${log_name_arr[i]}_${YESTERDAY}.log
        if [ $? -ne 0 ]; then
            echo "fail" >> $cutNginxLog_log
        else
            echo "success" >> $cutNginxLog_log
        fi
    done
}
#####################################

### function: Send USR1 signal to Nginx main process. The USR1 signal is to reopen the log file ###
function fSendTheSignalOfUSR1()
{
    pid_fullName=$1;
    echo "kill -USR1 $(cat ${pid_fullName}) result is " >> $cutNginxLog_log
    kill -USR1 $(cat ${pid_fullName})
    if [ $? -ne 0 ]; then
        echo "fail" >> $cutNginxLog_log
    else
        echo "success" >> $cutNginxLog_log
    fi
}
###################################################################################################

### function: gzip the yesterday log file ###
function fGzipLogFile()
{
    log_num=$1;
    log_path=$2;
    log_name_arr=($3);
    for((i=0;i> $cutNginxLog_log
        gzip -f ${log_path}${log_name_arr[i]}_${YESTERDAY}.log
        if [ $? -ne 0 ]; then
            echo "fail" >> $cutNginxLog_log
        else
            echo "success" >> $cutNginxLog_log
        fi
    done
}
#############################################

### function: delete the save days ago nginx log files ###
function fDelDaysAgoLogFile()
{
    log_path=$1;
    echo "find $log_path -mtime +$save_days -type f -name \*.log.gz |xargs rm -f result is " >> $cutNginxLog_log
    find $log_path -mtime +$save_days -type f -name \*.log.gz |xargs rm -f
    if [ $? -ne 0 ]; then
        echo "fail" >> $cutNginxLog_log
    else
        echo "success" >> $cutNginxLog_log
    fi
}
##########################################################



## set nginx log files you want to cut
echo "------------------------------" >> $cutNginxLog_log
nginx_log_name=($(fGetLogFilesName $nginx_log_dir))

## print the value of nginx_log_name
echo "nginx log files[($nginx_log_path)] is " >> $cutNginxLog_log
fPrintLogFilesName "${nginx_log_name[*]}"

## print the number of nginx_log_name array
nginx_log_num=${#nginx_log_name[*]}
echo "nginx_log_name array size is $nginx_log_num" >> $cutNginxLog_log


## cut the nginx log files
echo "------------------------------" >> $cutNginxLog_log
fCutLog $nginx_log_num $nginx_log_path "${nginx_log_name[*]}"


## Send USR1 signal to Nginx main process
echo "------------------------------" >> $cutNginxLog_log
fSendTheSignalOfUSR1 $nginx_log_pid


## gzip the yesterday log file
echo "------------------------------" >> $cutNginxLog_log
fGzipLogFile $nginx_log_num $nginx_log_path "${nginx_log_name[*]}"


## delete the save days ago nginx log files
echo "------------------------------" >> $cutNginxLog_log
fDelDaysAgoLogFile $nginx_log_path


echo "-------------------------------------------------------------" >> $cutNginxLog_log
echo "--------- end cut the log files $(date +"%Y-%m-%d %H:%M:%S") ---------" >> $cutNginxLog_log
echo "-------------------------------------------------------------" >> $cutNginxLog_log
echo "#############################################################" >> $cutNginxLog_log

좋은 웹페이지 즐겨찾기