Nginx cut logs by minute

Source: Internet
Author: User

There are many on-line logs that are cut by day. However, due to the application needs, the Web server for more detailed analysis! So to cut according to the minute. The requirements of the research and development department is that they have a tool that scans the logs, and the logs after the scan are completed. The suffix of complete, and all the scanned logs are moved to History_logs. Using two scripts to complete this requirement!



1. The script for the cut log is as follows:

Vim nginx_log.sh

#!/bin/bash# the Nginx log according to the minute cut # defines the path to the log file logs_path=/opt/nginx/logs/#定义nginx的进程号的路径pid_path =/opt/nginx/logs/ Nginx.pid# the current time of daydd= ' Date +%f ' #当前时间的小时HH = ' date +%h ' #当前时间的分钟mm = ' date +%m ' #将日志文件按照分钟切割的关键命令mv ${logs_path} Access.log ${logs_path}access-$dd-$HH-$mm. log# Kill the process number of the current minute while starting the next minute nginx process KILL-USR1 ' cat ${pid_path} '

The file needs to be placed in the/opt/nginx/sbin directory, so that it is straightforward to execute directly.

Because it is cut logs per minute, so we use the crontab command

#crontab-E

#该语句就是每分钟执行一次该脚本

*/1 * * * */opt/nginx/sbin/nginx_log.sh


We can observe that a large number of logs are generated in the logs directory and are similar to access-2015-09-08-10-39.log files. They were asked to have tools to scan this large number of logs, and finally all with the. Complete file we need to move to the corresponding date in the directory history_logs with logs siblings.



2. Move the script as follows:

Vim move_logs.sh

#! /bin/bash# definition log file directory logpath=/opt/nginx/logs/#前一天的时间lastdd = ' date-d ' Yesterday ' +%y-%m-%d ' #进入到该目录中cd/opt/nginx/ history_logs# Create log directory for the previous day mkdir $LASTDD # Start moving the log that was scanned the day before to the specified directory MV ${logpath}access-$LASTDD-*.complete $LASTDD/

The script needs to be executed in the wee hours of the day, and time is not necessary. Do not delay the day scan the day of the log can!


Also we need to place the script in the/opt/nginx/sbin directory

#crontab-E

* * * */opt/nginx/sbin/move_logs.sh

I set the time to execute the move script 12:30.

To this our in-minute cut script and move log file script complete!

Nginx cut logs by minute

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.