Linux Nginx log cutting and timed deletion of old log examples

Source: Internet
Author: User

In the Nginx log cutting aspect, my method and other people's methods have some different, but for me this method, if someone feels bad, can give me, thank you

nginx Log Cutting , I was directly written by a simple shell implementation, the contents are as follows:

The code is as follows Copy Code

Mv/usr/local/nginx/logs/abc.log/www/logbak/abc ' Date +%y%m%d%h%m '. Log
/usr/local/nginx/sbin/nginx-s Reload

After this execution, you will generate the log file in the/www/logbak directory, you can write the task of executing this shell in/etc/crontab, you can do it once in one hour, or you can perform it once a day, or you can perform it once a week, which depends on your own needs.


I am here every morning at 8 o ' Day, the resulting file format is this: Abc201111090801.log

So I found a way to delete the log file according to the rules of the file.

The code is as follows Copy Code

Rm-f/www/logbak/abc ' Date ' +%y%m%d '-d ' -10 Day ' *.log

This is also written in the shell script, and then every day to go back to/www/logbak directory to find the current time before the 10 days of files, and delete

Add one more example

1. Define Log roll Policy

  code is as follows copy code

Vim Nginx-log-rotate

/data/weblogs/*.log {
    nocompress
    Daily
    copytruncate
    Create
    notifempty
     Rotate 7
    olddir/data/weblogs/old_log
    Missingok
  & nbsp Dateext
    postrotate
       /bin/kill-hup ' Cat/var/run /nginx.pid 2>/dev/null ' 2>/dev/null | | True
    endscript
}

[Warning]/data/weblogs/*.log uses wildcard characters, all matching log files in the/data/weblogs/directory are cut. If you want to cut a particular log file, you specify the file. [/warning]

2. Set up Scheduled Tasks

The code is as follows Copy Code

# Vim/etc/crontab

* * * Root (/usr/sbin/logrotate-f/path/to/nginx-log-rotate)

This makes the log cut 23 59 minutes a day.

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.