Shell script for Nginx log cutting

Source: Internet
Author: User

By default, Nginx access logs are stored in a file. After a long time, the log content becomes unusually large. It is not conducive to archiving, backup, and analysis of log files. You can run scripts on a daily basis to automatically cut logs and save the daily access logs in a separate file.

To cut logs, you must first rename existing logs (the default access log is access. log, renamed to the date of the day), and then sends the USR1 signal to the Nginx master process to enable the process to re-open all log files and set the log file owner to the user running the work process, (because access. log has been renamed, so the process will create a new access. to record new access logs ). When the log file is successfully re-opened, the master will close all open log files and notify the work process to re-open the log file. The work process closes the previous log file in the correct way and uses the new log file to record it.

Before cutting:

[Root @ lnmp www] # du-h access. log
12 M access. log
[Root @ lnmp www] # head-n 1 access. log
120.27.47. *--[25/Sep/2015: 10: 35: 31 + 0800] "HEAD/HTTP/1.1" 200 0 "-" "Mozilla/5.0 (compatible; MSIE 9.0; windows NT 6.1; Win64; x64; Trident/5.0 )""-"
[Root @ lnmp www] # tail-n 1 access. log
112.126.75. *--[29/Sep/2015: 12: 06: 54 + 0800] "HEAD/HTTP/1.1" 200 0 "-" "Mozilla/5.0 (compatible; MSIE 9.0; windows NT 6.1; Win64; x64; Trident/5.0 )""-"
[Root @ lnmp www] #

Use the script to rename the log file and send the USR1 signal to the Nginx master process. Then add the script to the scheduled task:

Nginx master process number file storage location:

[Root @ lnmp scripts] # grep pid/application/nginx/conf/nginx. conf
Pid logs/nginx. pid;
[Root @ lnmp scripts] # cat/application/nginx/logs/nginx. pid
7039
[Root @ lnmp scripts] #

Path and file name of access logs in the configuration file:

[Root @ lnmp scripts] # grep access/application/nginx/conf/website/www. conf
Access_log/data/logs/website/www/access. log main;
[Root @ lnmp scripts]

Script:

# Cat rotate-nginx-logs.sh [root @ lnmp scripts] #
#! /Bin/bash
# Rotate nginx logs
# Nginx pid file:/application/nginx/logs/nginx. pid
# Nginx logs directory:/data/logs/website/www
# Default log name: access. log
# Author: Mr. Zhou

NGX_PID =/application/nginx/logs/nginx. pid
LOGS_DIR =/data/logs/website/www
LOG_NAME = access. log

Cd $ LOGS_DIR &&
/Usr/bin/rename $ LOG_NAME $ (/bin/date + % F-d 'Yesterday'). $ LOG_NAME &&
/Bin/kill-USR1 $ (cat $ NGX_PID)
[Root @ lnmp scripts] #

Add the script to the scheduled task and execute it at every day:

[Root @ lnmp scripts] # crontab-l
# Rotate nginx log everyday
00 00 ***/bin/bash/application/scripts/rotate-nginx-logs.sh &>/dev/null
[Root @ lnmp scripts] #

After the timer task runs the cut script automatically:

[Root @ lnmp www] # ls
2015-09-29.access.log access. log
[Root @ lnmp www] # tail-n 1 2015-09-29.access.log
112.126.75.174--[29/Sep/2015: 23: 59: 54 + 0800] "HEAD/HTTP/1.1" 200 0 "-" "Mozilla/5.0 (compatible; MSIE 9.0; windows NT 6.1; Win64; x64; Trident/5.0 )""-"
[Root @ lnmp www] # head-n 1 access. log
45.119.97.12--[30/Sep/2015: 00: 00: 31 + 0800] "POST/wp-cron.php? Doing_wp_cron = 1443542431.7503929138183593750000 HTTP/1.0 "200 0"-"" WordPress/4.3.1; http://www.111cn.net ""-"
[Root @ lnmp www] #

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.