In Linux, how does nginx cut access logs?

Source: Internet
Author: User

 

In linux, we can use crontab to regularly move access. log to the backup directory. At the same time, we can signal USR1 to the nginx main process to generate a new log file.

 

Before writing a script, perform the following assumptions:

 

The log file is/usr/local/nginx/logs/access. log.

 

The nginx master process id is saved in the file:/usr/local/nginx/logs/nginx. pid

 

Directory of the backup file after cutting:/usr/local/nginx/logs/bak/

 

For detailed steps of cutting, see the following operations:

 

1. Create the file cut_access_log.sh:

 

 

Vim/. bin/cut_access_log.sh

2. Input File cut_access_log.sh content

 

 

#! /Bin/bash

 

Org_path = "/usr/local/nginx/logs /"

 

Dest_path = "/usr/local/nginx/logs/bak /"

 

 

 

If [! -D "$ dest_path"]; then

 

Mkdir "$ dest_path"

 

Fi

 

 

 

Mv $ {org_path} access. log $ {dest_path} access $ (date + % Y % m % d). log

 

Kill-USR1 'cat/usr/local/nginx/logs/nginx. pid'

3. After saving cut_access_log.sh, grant the executable permission

 

 

 

Chmod + x/. bin/cut_access_log.sh

4. Compile the crontab file:

 

 

 

Vim/. cron

5. Enter the content of the crontab file and keep it

 

 

 

59 23 ***/bin/bash/. bin/cut_access_log.sh

6. Add to crontab

 

 

 

Crontab/. cron

 

 

In the preceding six steps, if you have sufficient permissions, access logs are cut at every day.

From the blog of the pure soul

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.