Detailed explanation of Nginx timed log cutting

Detailed explanation of Nginx timed log cutting

Preface

By default, Nginx logs are written to a file. In order to distinguish the logs under each domain, we usually store them separately. Even so, the file will become larger and larger, which is very inconvenient to view and analyze. Usually we do statistics on a daily basis. Let's talk about separating Nginx logs by date.

Configuration

Writing a Script

#!/bin/bash
#Initialize LOGS_PATH=/usr/local/nginx/logs
YESTERDAY=$(date -d "yesterday" +%Y%m%d)

#Cut logs by day mv ${LOGS_PATH}/bbs.52itstyle.com.access.log ${LOGS_PATH}/bbs.52itstyle.com.access_${YESTERDAY}.log
mv ${LOGS_PATH}/blog.52itstyle.com.access.log ${LOGS_PATH}/blog.52itstyle.com.access_${YESTERDAY}.log

#Send USR1 signal to the nginx main process to reopen the log file, otherwise it will continue to write data to the file after mv. The reason is: in the Linux system, the kernel looks for files based on file descriptors. Failure to do so will result in log rotation failure.
kill -USR1 `ps axu | grep "nginx: master process" | grep -v grep | awk '{print $2}'`

#Delete the logs from 7 days ago cd ${LOGS_PATH}
find . -mtime +7 -name "*20[1-9][3-9]*" | xargs rm -f

exit 0

Write Task

#Execute command crontab -e
#Write to file and save 0 0 * * * /home/scripts/cut_del_nginx_logs.sh

crontab

Crond is a daemon process used in Linux to periodically execute certain tasks or wait for certain events to be processed. It is similar to the scheduled tasks in Windows. When the operating system is installed, this service tool will be installed by default and the crond process will be automatically started. The crond process will periodically check every minute whether there are tasks to be executed. If there are tasks to be executed, the task will be automatically executed. Task scheduling under Linux is divided into two categories: system task scheduling and user task scheduling.

System task scheduling: The work that the system needs to perform periodically, such as writing cache data to the hard disk, cleaning up logs, etc. There is a crontab file in the /etc directory, which is the configuration file for system task scheduling.

Crontab service installation

Install crontab:

yum install crontabs

Service Operation Instructions:

service crond start //Start the service service crond stop //Shut down the service service crond restart //Restart the service service crond reload //Reload the configuration

Check the crontab service status:

service crond status

Manually start the crontab service:

service crond start

Check whether the crontab service has been set to start at boot time by executing the command:

ntsysv

Add automatic startup:

chkconfig –level 35 crond on

crontab format description

In the crontab file created by the user, each line represents a task, and each field in each line represents a setting. Its format is divided into six fields. The first five segments are time setting segments, and the sixth segment is the command segment to be executed. The format is as follows:

In each of the above fields, the following special characters can also be used:

  • Asterisk (*): represents all possible values. For example, if the day field is an asterisk, it means that the command operation is executed every day after the constraints of other fields are met.
  • Comma (,): You can specify a list range of values ​​separated by commas, for example, "1,2,5,7,8,9"
  • Middle bar (-): You can use the middle bar between integers to represent a range of integers, for example, "2-6" means "2,3,4,5,6"
  • Forward slash (/): You can use a forward slash to specify the frequency of the time interval, for example, "0-23/2" means execution every two hours. At the same time, forward slashes can be used together with asterisks, for example, */10, if used in the minute field, means execution every ten minutes.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of Nginx log cutting by date (cutting by day)
  • nginx log cutting shell script
  • Detailed explanation of Nginx log configuration and log cutting
  • Nginx log module and log timing cutting method
  • How to write a script to cut nginx logs every day under Linux system
  • nginx log cutting script sharing
  • Detailed explanation of nginx log cutting implementation
  • Detailed process of Nginx using Logrotate log segmentation

<<:  How to use vue filter

>>:  The difference between MySQL database host 127.0.0.1 and localhost

Recommend

Nginx improves access speed based on gzip compression

1. Why does nginx use gzip? 1. The role of compre...

Notes on MySQL case sensitivity

Table of contents MySQL case sensitivity is contr...

Docker network mode and configuration method

1. Docker Network Mode When docker run creates a ...

Example of how to deploy MySQL 8.0 using Docker

1. Refer to the official website to install docke...

Implementation steps for docker deployment lnmp-wordpress

Table of contents 1. Experimental Environment 2. ...

When MySQL is upgraded to 5.7, WordPress reports error 1067 when importing data

I recently upgraded MySQL to 5.7, and WordPress r...

7 useful new TypeScript features

Table of contents 1. Optional Chaining 2. Null va...

Linux editing start, stop and restart springboot jar package script example

Preface In the springboot configuration file, the...

Detailed tutorial on installing mysql-8.0.20 under Linux

** Install mysql-8.0.20 under Linux ** Environmen...

mysql group_concat method example to write group fields into one row

This article uses an example to describe how to u...

6 ways to view the port numbers occupied by Linux processes

For Linux system administrators, it is crucial to...

Implementation of pushing Docker images to Docker Hub

After the image is built successfully, it can be ...

Detailed explanation of sql_mode mode example in MySQL

This article describes the sql_mode mode in MySQL...