The Shell script separates Nginx logs and regularly cleans them.

Source: Internet
Author: User
: This article mainly introduces how to split Nginx logs using Shell scripts and regularly clean up Nginx logs. For more information about PHP tutorials, see. By default, nginx generates only one access log. during daily accumulation, the log file will become very large. if you need to analyze the log, whether it is using script analysis, it is not convenient to download logs for local analysis. Splitting Nginx access logs every day facilitates log analysis.

I. create a script file

Vim cut_nginx_log.sh
 
 
  1. #! /Bin/bash
  2. # Nginx access log segmentation shell script
  3. # Www.webyang.net
  4. # Log Directory
  5. Log_dir = "/usr/local/nginx/logs /"
  6. Cd $ log_dir
  7. Time = 'date + % Y % m % d -- date = "-1 day "'
  8. # Nginx Startup directory
  9. Nginx_dir = "/etc/init. d/nginx"
  10. # Log Retention days
  11. Save_days = 15
  12. # Location where the log file name is located '/', key (number of '/' + 1)
  13. Num = 6;
  14. # Here to be optimized (get the file name without a suffix)
  15. Website = 'ls $ log_dir *. log | xargs-n 1 | cut-f 1-d "." | cut-f $ num-d "/"'
  16. # All log files in the log directory are folders, and corresponding time files are created cyclically.
  17. For I in $ website
  18. Do
  19. # Determine whether a directory exists
  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. # Delete data before the specified number of days
  26. Find $ log_dir/-mtime + $ save_days-exec rm-rf {}\;
  27. # Enable nginx smoothly
  28. $ Nginx_dir reload
2. set crontab and cut regularly every day
If not, run yum-y install crontabs.
Enter the crontab-e command (for details, refer to: http://www.webyang.net/Html/web/article_232.html ),
Enter the following content:
00 00 ***/bin/bash/usr/local/nginx/cut_nginx_log.sh
1. 00 means 00: 00, that is, A.M.. you do not need to define "***" as "day, month, and year ".
2. "/usr/local/nginx/cut_nginx_log.sh" is your shell path.

OK! At every day, the log is automatically cut and stored in a directory named after the website log file.

III. Presentation
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 preceding section describes how to use Shell scripts to split Nginx logs and regularly clean up the logs, including some content. I hope you will be helpful to anyone interested in PHP tutorials.

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.