nginx log cutting and log cleaning

########################## log cutting ###################### ##########
1, upload script to / usr / local / nginx / logs / under
2, along with permission to execute
the chmod + X cut_nginx_log.sh

. 3, prepared by the timer task execution cut_nginx_log 0:00 per day. sh script
the crontab -e
0 0 * * * the root /usr/local/nginx/logs/cut_nginx_log.sh
or
Vim / etc / the crontab
0 0 * * * the root /usr/local/nginx/logs/cut_nginx_log.sh

. 4, the script content:
CAT cut_nginx_log.sh

# / bin / bash!
# log storage path

Log_path = ¥ NGINX_WORKDIR # / logs
LOGS_PATH = / usr / local / nginx / logs
# acquisition time yesterday
YESTERDAY = $ (date -d "yesterday " +% Y-% m-% d)

month_age=${date -d "-30 days" +%Y-%m%-%d}

if ! cd $LOG_PATH; then

     exit 2

be

 


The PID #nginx path
NGINX_PID = / usr / local / Nginx / logs / nginx.pid
# the current access.log / error.log rename access.yesterday.log / error.yesterday.log
Music Videos LOGS_PATH} {$ / {} $ LOGS_PATH the access.log / Access. YESTERDAY} {$ .log
Music Videos LOGS_PATH} {$ {$ /error.log LOGS_PATH} / error. YESTERDAY} {$ .log
# Nginx transmits a signal to the main process USR1. USR1 signal log file is re-opened
the kill -USR1 $ (CAT NGINX_PID $ {})

########################### log cleaning ### ##############################

1, upload the script to / usr / local / nginx / logs / under
2, together with the implementation of permissions
chmod + rm_nginx_logfile.sh the X-

3, the preparation of regular tasks every day 0:00 rm_nginx_logfile.sh script
crontab -e
0 0 * * * root /usr/local/nginx/logs/rm_nginx_logfile.sh
or
vim / etc / crontab
0 0 * * * root /usr/local/nginx/logs/rm_nginx_logfile.sh

rm_nginx_logfile.sh CAT


#! / bin / bash
path where logs #
LOGS_PATH = / usr / local / nginx / logs
# Set the retention time unit (days)
SAVE_TIME = 30
# command to perform the final
# at the same time find the change log 30 days ago at the end of the log file and then delete
# If you do not -name option after over 30 scripts will be removed
the Find LOGS_PATH $ {} / $ {SAVE_TIME -mtime +} -name \ * log |. xargs RM -rf {}
 
--- ------------------
Disclaimer: This article is CSDN blogger "Young Bu sin - Mahone 'original article, follow the CC 4.0 by-sa copyright agreement, reproduced attach the original source link and this statement.
Original link: https: //blog.csdn.net/bj_lsq/article/details/84837399

Guess you like

Origin www.cnblogs.com/guanghuiqq/p/11343826.html
Recommended