The script is as follows:
#! /Bin/bash
# Nginx access log segmentation shell script
# Log Directory
Log_dir = "/usr/local/nginx/logs /"
Cd $ log_dir
Time = 'date + % Y % m % d -- date = "-1 day "'
# Log Retention days
Save_days = 15
# Location where the log file name is located '/', key (number of '/' + 1)
Num = 6;
# Here to be optimized (get the file name without a suffix)
Website = 'ls $ log_dir *. log | xargs-n 1 | cut-f 1-d "." | cut-f $ num-d "/"'
# All log files in the log directory are folders, and corresponding time files are created cyclically.
For I in $ website
Do
# Determine whether a directory exists
If [! -D $ log_dir $ I]; then
Mkdir "$ I"
Fi
Cp $ log_dir $ I. log $ log_dir $ I/$ I-$ time. log
True> $ log_dir $ I. log
Done
Find $ log_dir-mtime + $ save_days-exec rm-rf {}\;
Set crontab and run it once every morning.
Demonstration: web1.log => web1/web1-20160322.log web1/web1-20160323.log web2.log => web2/web2-20160322.log web2/web2-20160323.log
Other methods include cronolog and logrotate.