nginx 日志按日切割脚本

回复 收藏
#! /bin/bash
datedir=`date +%Y%m%d`
/bin/mkdir  /home/logs/$datedir >/dev/null 2>&1
/bin/mv /home/logs/*.log /home/logs/$datedir
#/bin/kill -HUP `cat /var/run/nginx.pid`
2009-10-09 16:56 举报
已邀请:
0

Kry1702

赞同来自:

1
0

summer123

赞同来自:

附上我写的nginx的日志切割脚本。 测试OK
#!/bin/bash
#
# author : caimengzhi
# date   : 2016-06-16
# qq     : 610658552
# fuction: backup the nginx push server acccess and error log
# input  : the current access.log error.log
# output : cp the current access.log error.log to the current direction file_logs and named end with the current date
# backup time : 0 3 * * *  backup at Daily 3:00

pushlog_path=/usr/local/nginx_push/logs
nginx_push_backup_file_logs=file_logs
backup_nginx_push_server_script_log=/home/admin_loocha11/caimz/script/shell/nginx_rtmp/push
nginx_pid=$(ps aux |grep "nginx_push" | grep -v grep | wc -l)
# jugde the nginx_push server status from the numbers nginx_push process
if [ $nginx_pid -eq 0 ];then
    echo "nginx push server is not start."                             >> ${backup_nginx_push_server_script_log}/scrpit_backup_clean_the_log.log
    exit 1
else
    echo "nginx push server is starting..."                            >> ${backup_nginx_push_server_script_log}/scrpit_backup_clean_the_log.log
fi

# judge the nginx_push server logs dircetions
if [ -d $pushlog_path ];then
    echo "nginx_push of logs direction is exist."                      >> ${backup_nginx_push_server_script_log}/scrpit_backup_clean_the_log.log
    cd /usr/local/nginx_push/logs
    if [ -d $nginx_push_backup_file_logs ];then
        echo " the nginx_push of file_logs is exist"                   >> ${backup_nginx_push_server_script_log}/scrpit_backup_clean_the_log.log
    else
        mkdir file_logs
    fi
else
    echo "nginx_push of logs direction is not exist."                  >> ${backup_nginx_push_server_script_log}/scrpit_backup_clean_the_log.log
    exit 1
fi

# backup the access.log file
cd /usr/local/nginx_push/logs
if [ -f access.log ];then
    cp access.log  file_logs/access_$(date "+%Y_%m_%d_%H:%M:%S").log
    > access.log
else
    echo "the current access.log is not exist or this file is zero.pls check the log file." >> ${backup_nginx_push_server_script_log}/scrpit_backup_clean_the_log.log
    exit 1
fi

#backup the error.log file
cd /usr/local/nginx_push/logs
if [ -f error.log ];then
    cp error.log  file_logs/error_$(date "+%Y_%m_%d_%H:%M:%S").log
    > error.log
else
    echo "the current error.log is not exist.pls check the log file."   >> ${backup_nginx_push_server_script_log}/scrpit_backup_clean_the_log.log
    exit 1
fi
echo "------This is the end. delimiter Now time:`date`-------------- "  >> ${backup_nginx_push_server_script_log}/scrpit_backup_clean_the_log.log
echo "                                                               "  >> ${backup_nginx_push_server_script_log}/scrpit_backup_clean_the_log.log
0

午夜DJ

赞同来自:

学习
0

出VU时代

赞同来自:

应该还要有日志的有效期。比如一个月以前的日志自动删除!

回复帖子,请先登录注册

退出全屏模式 全屏模式 回复
评分
可选评分理由: