Shell script splits Nginx logs and cleans up regularly

Source: Internet
Author: User
The default nginx will only generate an access log, in the accumulation of daily, log files will become very large, if you need to do a log analysis, whether it is using script analysis, or the log download local analysis, is not very convenient. The daily partition of Nginx access logs facilitates the analysis of logs.

A: Create a new script file

Vim cut_nginx_log.sh
 
 
  1. #!/bin/bash
  2. #nginx Access Log Segmentation shell script
  3. #www. webyang.net
  4. #日志目录
  5. Log_dir = "/usr/local/nginx/logs/"
  6. CD $log _dir
  7. Time = ' Date +%y%m%d--date= '-1 Day '
  8. #nginx启动目录
  9. Nginx_dir = "/etc/init.d/nginx"
  10. #日志保存天数
  11. save_days = the
  12. #日志文件名所处 '/' Split position, key ('/' number +1)
  13. Num = 6 ;
  14. #此处待优化 (get file name without suffix)
  15. website = ' ls $log _dir*.log | xargs-n 1 | cut-f 1-d "." | cut-f $num-D "/" '
  16. #日志目录下所有的日志文件为文件夹, cycle the new time file
  17. for I inch $website
  18. Do
  19. #判断目录是否存在
  20. if [! - d $log _dir$i ]; Then
  21. mkdir "$i"
  22. Fi
  23. MV $log _dir$i . Log $log _dir$i / $i - $time . Log
  24. Done
  25. #删除设置天数前的数据
  26. Find $log _dir / - Mtime + $save _days - exec RM - RF {} \;
  27. #平滑启动nginx
  28. $nginx _dir Reload
Second, set crontab, daily cutting
If it is not installed, run the Yum-y install Crontabs installation.
Enter the CRONTAB-E command (detail reference: http://www.webyang.net/Html/web/article_232.html),
Enter the following content:
XX * * * */bin/bash/usr/local/nginx/cut_nginx_log.sh
1, 00 00 means 00 Points 00 points, that is, 0 o'clock in the morning, the back "* * *" for "Sun and Moon Year" does not need to define
2. "/usr/local/nginx/cut_nginx_log.sh" is the path of your shell.

Ok! When you're done, the log is automatically cut at 0 o'clock every day and the corresponding directory is named after the site log file.

Third, display
Web1.log = Web1/web1-20160322.log web1/web1-20160323.log
Web2.log = Web2/web2-20160322.log web2/web2-20160323.log

Blog: http://www.webyang.net/Html/web/article_255.html

The above describes the shell script to split the Nginx log, and regularly clean up, including the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.