Nginx log cut

1. Write a log script

#!/bin/bash
#This script needs to be run at 00:00 every day

#Nginx log file Storage path
logs_path=”/usr/local/nginx/logs”

#Create folder
mkdir -p ${logs_path}/$(date -d “yesterday” +”% Y”)/$(date -d “yesterday” +”%m”)/

mv ${logs_path}/access.log ${logs_path}/$(date -d “yesterday” +” %Y”)/$(date -d “yesterday” +”%m”)/access_$(date -d “yesterday” +”%Y%m%d”)
kill -USR1 $(cat /usr /local/nginx/logs/nginx.pid)

2, upload the script to /usr/local/nginx/logs/
3, attach the execution permission
chmod +x cut_nginx_log. sh

4. Write timing tasksExecute cut_nginx_log.sh script at 0:00 every day
crontab -e
0 0 * * * root /usr/local/nginx/logs/cut_nginx_log.sh
Or
vim /etc/crontab
0 0 * * * root /usr/local/nginx/logs/cut_nginx_log.sh

Or enter the command vi /etc/crontab

Add configuration at the end of the file (start at 14:24)

24 14 * * * root /usr/local/nginx/logs/cut_nginx_log.sh


###########################Log Cleanup (Untested)################ ################

1. Upload the script to /usr/local/nginx/logs/
2. Attach execution Permission
chmod +x rm_nginx_logfile.sh

3, write timed tasks every 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

cat rm_nginx_logfile.sh

#!/bin/bash#Log path LOGS_PATH=/ usr/local/nginx/logs#Set the retention time Unit (days) SAVE_TIME=30#Execute the final command #Find the file whose modification log ends with log at the same time 30 days ago and delete it#If there is no -name option, script after 30 Will be deleted find ${LOGS_PATH}/ -mtime +${SAVE_TIME} -name \*.log |xargs rm -rf {}

Leave a Comment

Your email address will not be published.