Achieve Nginx log splitting!

The longer the higher memory usage, high concurrency, the server will bring enormous pressure cache

Workaround: Create a new directory, set up regularly scheduled periodic movement of the log old directory to the new directory, usually saves only 30 days, will be deleted after 30 days

[root @ localhost ~] # date "+% Y% m% d" // day time
20,190,913
[the root @ localhost ~] -d # DATE "Day -1" "the Y +% m% D%" // yesterday time
20,190,912

=====================================================================================

[root@localhost ~]# vim /opt/cut_nginx_log.sh

#! / bin / the bash 
# cut_nginx_log.sh 

datetime = $ (DATE -d " -1 Day "  " + the Y% m% D% " )         // time, date -d "-1 day" Save representing the date. 1 
log_path, = " / usr / local / nginx / logs "              storage location // log 
pid_path = " /usr/local/nginx/logs/nginx.pid "    // process PID number, there are PID number represents the process is still alive 
[ -d $ log_path / Backup] || mkdir -p log_path $ / Backup    // if $ log_path / backup is not a directory is created $ log_path / backup directory
 IF [- f $ pid_path]                                 // if $ pid_path is a file 
then 
mv $ log_path$ log_path /access.log / Backup / access.log- $ datetime   // then moving old log files to a new directory in order to achieve split 
the kill - USR1 $ (CAT $ pid_path)                                     // USR1 signal behalf, he will give process pass a signal for the process to generate a new log 
the Find $ log_path / Backup -mtime + 30 | xargs RM - f                 // find the log older than 30 days and remove
 the else 
echo " Error, Nginx iS not Working! " | TEE -a / var / log / messages   // otherwise prompt nginx is not working and is appended to the log 
fi

[root@localhost ~]# chmod +x /opt/cut_nginx_log.sh
[root@localhost ~]# bash /opt/cut_nginx_log.sh
[root@localhost ~]# cd /usr/local/nginx/logs/
[root@localhost logs]# ls
access.log   backup  error.log  nginx.pid
[root@localhost logs]# ls backup/
access.log-20190912

[root @ localhost ~] # crontab -e
crontab: Installing new new crontab
[root @ localhost ~] # crontab the -l
0 0 * * * bash /opt/cut_nginx_log.sh // monthly weekly daily 0:00 execution script     

 

 

When not running!

[root@localhost logs]# killall -9 nginx
[root@localhost logs]# bash /opt/cut_nginx_log.sh
/opt/cut_nginx_log.sh: 第 11 行:kill: (8282) - 没有那个进程
[root@localhost logs]# rm -rf /usr/local/nginx/logs/nginx.pid
[root@localhost logs]# bash /opt/cut_nginx_log.sh
Error,Nginx is not working!

 

Guess you like

Origin www.cnblogs.com/cxm123123form/p/11516088.html