Get the App
SLTechnology News&Howtos  ›  Servers  › 

Nginx regularly splits the log

Shulou Source: shulou.com Published: 2022-06-03 05:43:51 09月21日 Update

It has been a headache for nginx logs to grow over time, but we can actually send a signal USR1 to the nginx main process to generate new log files, and then archive the old log files. Here is the script I used to split the logs.

[root@master ~] # cat / HomeAccord TestCharger binbinqqinxpostreloadlogfile.shandhelplash Bash # log_pathbase_path='/usr/local/nginx-1.12.1/logs'# get year and monthyear_month=$ (date-d yesterday + "% Y% m") # get yesterdayday=$ (date-d yesterday + "% d") # create backup directorymkdir-p $base_path/$year_month# copy logfile to backupmv $base_path/access.log $base_path/$year_month/access_$day .log # echo $base_path/$year_month/access_$day.log# reload logfilekill-USR1 `cat / usr/local/nginx-1.12.1/logs/ nginx.pid`

Add a scheduled task to crontab and execute the script to split the log at 23:59 every day

# nginx access log backup59 23 * sh / home/ysyf/bin/nginx_reload_logfile.sh

The effect of slicing is as follows, of course, you can also customize the path you like and modify the script.

[root@master / usr/local/nginx-1.12.1/logs] # ll Total usage 490828drwxr-xr-x 2 root root 4096 October 1 23:59 201909drwxr-xr-x 2 root root 4096 October 31 23:59 201910 error.log-rw-r--r-- error.log-rw-r--r-- 1 root-1 nobody root 2477169 November 1 23:24 access.log-rw-r--r-- 1 nobody root 499617441 November 1 23:24 Root 6 December 31 2018 nginx.pid [root@master / usr/local/nginx-1.12.1/logs] # cd 201910 [root@master / usr/local/nginx-1.12.1/logs/201910] # ll Total usage 64348 nobody root nobody root 955541 October 3 23:53 access_02.log-rw-r--r -- 1 nobody root 1328787 October 4 23:46 access_03.log-rw-r--r-- 1 nobody root 533591 October 5 23:54 access_04.log-rw-r--r-- 1 nobody root 1048041 October 6 23:59 access_05.log-rw-r--r-- 1 nobody root 402311 October 7 23:56 access_06.log-rw-r--r-- 1 nobody root 3036879 October 8 23:58 access_07.log-rw -nobody root nobody root 2848916 October 9 23:48 access_08.log-rw-r--r-- 1 nobody root 2814881 October 10 23:58 access_09.log-rw-r--r-- 1 nobody root 3424007 October 11 23:51 access_10.log

Tags: Logs scripts files usage headaches tasks signals effects time paths processes growth generation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi MySQL Apple Shulou Technology Linux