Use shell scripts in nginx to regularly cut logs every day

Source: Internet
Author: User

Nginx does not support using cronolog to rotate logs like Apache, but you can use the following methods to cut log files:
Mv/data1/logs/access. log/data1/logs/20111030.log
Kill-USR1 Nginx master process number

Use the mv command to rename the log file to/data1/logs/20111030.log, and then send the kill-USR1 signal to the main process ID of Nginx to enable Nginx

Generate a new log file/data1/logs/access. log.

To regularly cut logs every day, you also need to use crontab. We can write a daily cut log by year or month.
Shell script for storing logs in directories:

 

The code is as follows: Copy code

#! /Bin/bash
# This script run

# The Nginx logs path
Logs_path = "/home/www/data/logs /"

Mkdir-p $ {logs_path} $ (date-d "yesterday" + "% Y")/$ (date-d "yesterday" + "% m ")/
Mv $ {logs_path} access. log $ {logs_path} $ (date-d "yesterday" + "% Y")/$ (date-d "yesterday" + "%

M ")/access _ $ (date-d" yesterday "+" % Y % m % d "). log
Kill-USR1 'cat/usr/local/servers/nginx. Pid'

Grant executable permissions:

Run crontab-e to enter the editing status.

The code is as follows: Copy code

00 04 ***/root/cut_logs.sh

In this way, your system will automatically cut nginx logs at every morning.

If you want to change the other time, you can modify 00 04 ***/root/cut_logs.sh, for example, to cut nginx access at every day.

Question Log

Crontab-e

Enter the following content:

The code is as follows: Copy code

00 00 ***/bin/bash/usr/local/nginx/sbin/cut_nginx_log.sh

You only need to modify the preceding number.

Related Article

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.