nginx scheduled task split log

Nginx is installed in the /usr/local folder

1. Create a file data in the nginx root directory and save the script file runlog.sh
mkdir /data
vim ./data/runlog.sh

2. Add the following code to the script file runlog.sh file.
The meaning of the code is: In the logs folder, a log file of yesterday (named: access_YYYY-MM-DD.log) is generated at one o'clock in the morning every day, and saved to the folder of the corresponding month of the corresponding year (created with the year YYYY) folder, and then create a folder by month MM in YYYY, and then save the log file to MM)
#!/bin/bash
log_base=/usr/local/nginx/logs
log_path=${log_base}/$(date -d yesterday +%Y)/$(date -d yesterday +%m)
mkdir -p $log_path
mv $log_base/access.log $log_path/access_$(date -d yesterday +%Y%m%d).log
touch $log_base/access.log
kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
#echo $(date -d yesterday +%Y%m%d).log

3. Run and check runlog.sh
, remove the # sign in front of echo, save and exit the runlog.sh script, and enter directly:
sh runlog.sh //Execute the script to check for errors

4. Execute runlog.sh regularly and
enter the command:
crontab -e

Enter the following information in the opened interface:
1 0 * * * sh /usr/local/nginx/data/runlog.sh //execute every day at 0:1 a.m.

Note: Yesterday can also be used today, etc., just change the execution time of the scheduled task

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326098671&siteId=291194637