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

Robots.txt detailed introduction

Robots.txt is a plain text file in which website ...

Using text shadow and element shadow effects in CSS

Introduction to Text Shadows In CSS , use the tex...

Unbind SSH key pairs from one or more Linux instances

DetachKeyPair Unbind SSH key pairs from one or mo...

Summary of some situations when Docker container disk is full

Preface This article describes two situations I h...

NodeJS realizes image text segmentation

This article shares the specific code of NodeJS t...

CSS3 realizes the animation of shuttle starry sky

Result: html <canvas id="starfield"&...

Basic usage of UNION and UNION ALL in MySQL

In the database, both UNION and UNION ALL keyword...

Detailed explanation of dynamic Christmas tree through JavaScript

Table of contents 1. Animated Christmas Tree Made...

Native js to achieve accordion effect

In actual web page development, accordions also a...

Steps to completely uninstall the docker image

1. docker ps -a view the running image process [r...

Solution to 700% CPU usage of Linux process that cannot be killed

Table of contents 1. Problem Discovery 2. View de...

jenkins+gitlab+nginx deployment of front-end application

Table of contents Related dependency installation...

Build a file management system step by step with nginx+FastDFS

Table of contents 1. Introduction to FastDFS 1. I...

Vue uses dynamic components to achieve TAB switching effect

Table of contents Problem Description What is Vue...