Nginx log split by date (split by day)

Source: Internet
Author: User
Tags php script
The following is a nginx log cut script, cut by day
$ (date +%d) Get the day of the month, such as May 18, will be taken out is 18
In this way, you can do the monthly cycle, only keep one months of the log, and do not worry about cleaning up the expired log
Script please set execution in crontab, such as log-cutting script at 23:59 every day
* * */usr/local/nginx/nginxlog_happy.sh

The following red words are script content:
Savepath_log the path to save the log
Nginxlogs current Nginx Default log, if you are installed in the/usr/local/nginx directory, the log is the same as the script vim/usr/local/nginx/nginxlog_happy.sh ( Use the VIM command to create a new bash log split script Nginxlog_happy.log to use for timed split logs: (note that in the actual use, please delete the bold part of the Chinese comment, the following comments are designed to facilitate your reading)
#/bin/bash
#Site: Happy #Email: happy.yin@qq.com
yesterday=$ (Date +%y-%m-%d) #取得服务器当前时间
savepath_log= '/usr/local/nginx/logs/happylogs ' #分割后的日志文件保存目录 (Note the end of the list of slashes removed)nglogs= '/usr/local/nginx/logs ' #nginx日志文件目录, replace it with the actual address (trailing slash to avoid the "/" repeat in the following path)
#判断分割日志保存目录是否存在不存在则创建if [!-D ${savepath_log}] # Note the spaces between statements cannot be omitted, this is a bash script that is not as easy-going as a PHP scriptThen mkdir-p ${savepath_log} fi #通过mv命令将日志移动到分割后的日志目录里, and then sends the KILL-USR1 signal to Nginx's main process number, allowing Nginx to regenerate a new log file. MV ${nglogs}/access.log ${savepath_log}/access_${yesterday}.log #具体根据你的实际日志文件名路径 to modify Access.log nameMV ${nglogs}/error.log ${savepath_log}/error_${yesterday}.log #具体根据实际日志文件名地址 Modify Error.log nameMV ${nglogs}/host.access.log ${savepath_log}/host.access_${yesterday}.log
KILL-USR1 $ (cat/usr/local/nginx/logs/nginx.pid) #通知nginx重新生成新的日志
Here's the most important step: (We recommend a second way to modify) crontab-e in the inside enter the following: (log segmentation every 23:59 P.M., pay attention to the middle of the space, if the command can not find the Crontab file selection of the following ways to modify it) * * * /usr/local/nginx/nginxlog_happy.shIf the above command cannot find the file address, use the following command to find the crontab file for modification: Vim/etc/crontabAdd on last line
* * * /usr/local/nginx/nginxlog_happy.sh Restart Crond to make settings take effect /etc/rc.d/init.d/crond Restart#yum install-y Vixie-cron Installation Schedule task, some systems may not have preinstalled required to perform this operation for installation
Chkconfig Crond on #设为开机启动
Service Crond Start #启动
If you copy directly, pay attention to the problem of font encoding, many windows on the script uploaded to Linux is wrong, or because of coding problems

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.