Detailed explanation of the implementation process of Nginx log timing splitting in CentOS 7

Detailed explanation of the implementation process of Nginx log timing splitting in CentOS 7

1. Write a split script (splitNginxLog.sh)

* Because this example sets the log splitting to be performed at 0:00 every day, folder and rq are both set to use yesterday's date for archiving.

#!/bin/bash
folder=`date -d yesterday +%Y%m`
rq=`date -d yesterday +%Y%m%d`
# Original log path logs_path="/var/log/nginx/sitename.com/"
# Log backup path logs_backup_path="/var/log/nginx/sitename.com/$folder"
# Logs to be split logs_access="access"
logs_error="error"
# Create a backup path [ -d $logs_backup_path ]||mkdir -p $logs_backup_path
# Move the logs to the backup folder mv ${logs_path}${logs_access}.log ${logs_backup_path}/${logs_access}_${rq}.log
mv ${logs_path}${logs_error}.log ${logs_backup_path}/${logs_error}_${rq}.log
#Terminate nginx's pid
pid_path="/var/run/nginx.pid"
kill -USR1 $(cat $pid_path)

2. Test log splitting script

Run the following statement in the directory where the .sh file is stored to test whether the split script is successful.

# sh splitNginxLog.sh

If you get the error $'\r': command not found, it may be due to the encoding of the .sh file, because the script written in the Windows editor is slightly different from that in Linux.

Treatment method:

1. Install dos2unix for encoding conversion

# yum install dos2unix

2. Convert code

# dos2unix splitNginx.sh

3. Set up automatic execution

# crontab -l //List all scheduled tasks
# crontab -e //Edit scheduled tasks

Write the following statements in it:

00 00 * * * bash /root/splitNginxLog.sh

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:
  • How to install PHP7.4 and Nginx on Centos
  • Detailed tutorial for installing nginx on centos8 (picture and text)
  • CentOS 7.2 builds nginx web server to deploy uniapp project
  • Solve the problem of "Welcome to nginx on Fedora!" after installing nginx on Centos7, and there is no default.conf file in the conf.d directory
  • Centos7.3 How to install and deploy Nginx and configure https
  • How to add Nginx to system services in CentOS7
  • How to install Nginx in CentOS7 and configure automatic startup
  • Centos7 startup process and Nginx startup configuration in Systemd
  • How to build a private server in docker (docker-registry with nginx&ssl on centos)

<<:  A brief discussion on this.$store.state.xx.xx in Vue

>>:  MySQL table auto-increment id overflow fault review solution

Recommend

How to use port 80 in Tomcat under Linux system

Application Scenario In many cases, we install so...

SQL merge operation of query results of tables with different columns

To query two different tables, you need to merge ...

Let you understand the deep copy of js

Table of contents js deep copy Data storage metho...

UDP simple server client code example

I won’t go into details about the theory of UDP. ...

A brief analysis of MySQL locks and transactions

MySQL itself was developed based on the file syst...

Analyze Mysql transactions and data consistency processing issues

This article analyzes the consistency processing ...

JavaScript function detailed introduction

Any number of statements can be encapsulated thro...

A brief discussion on the implementation of fuzzy query using wildcards in MySQL

In the MySQL database, when we need fuzzy query, ...

Six ways to reduce the size of Docker images

Since I started working on Vulhub in 2017, I have...

JavaScript Document Object Model DOM

Table of contents 1. JavaScript can change all HT...

A brief analysis of Linux network programming functions

Table of contents 1. Create a socket 2. Bind sock...

JavaScript super detailed implementation of web page carousel

Table of contents Creating HTML Pages Implement t...

Detailed steps to build an independent mail server on Centos7.9

Table of contents Preface 1. Configure intranet D...

Uniapp's experience in developing small programs

1. Create a new UI project First of all, our UI i...

Solution to MySQL master-slave delay problem

Today we will look at why master-slave delay occu...