#!/bin/bash
# 01 00 * * * /nginxlogs/ngx_logcut.sh >/dev/null 2>&1 ##Can be placed in scheduled tasks to automatically execute the script
pidfile=/var/run /nginx.pid #nginx process pid file
logpath='/nginxlogs/' #Log directory
keepdays=30
logfiles=(error.log access.log) #With List the log names in array form
cd $logpath #Enter the log directory
for logfile in ${logfiles[@]}; do #Match in array form
if [ ! -e $logfile ];
then
continue
fi
find . -type f -name $logfile"20*" -mtime +$keepdays -exec rm {} ; #find finds the log that meets the conditions and deletes it
mv $logfile $logfile$(date - d "yesterday" +"%Y%m%d") #Change yesterday's log to the format of log name + date
done
kill -USR1 `cat $pidfile` ##USR1 is usually used to inform the application to reload the configuration file; for example, the nginx server sending a USR1 signal will cause the following steps to occur: stop accepting new connections, wait for the current connection to stop, reload the configuration file, reopen the log file, restart the server, thereby achieving a relatively smooth non-shutdown Change
The above introduces the nginx log splitting script, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.