Nginx Log Cutting shell script _linux shell

Source: Internet
Author: User

First, the script idea

The first step is to rename the log file without having to worry about renaming the log file and losing the log when Nginx is not found. Before you reopen the log file for the original name, Nginx still writes to the file you renamed, and Linux relies on the file descriptor instead of the filename to locate the file.
The second step sends the USR1 signal to the Nginx main process.
Nginx The main process receives a signal, it reads the log file name from the configuration file, opens the log file (named after the log name in the configuration file), and takes the user of the worker process as the owner of the log file.
Once the log file is reopened, the Nginx main process closes the duplicate log file and notifies the worker process to use the newly opened log file.
The worker process immediately opens a new log file and closes the log file with the duplicate name.
Then you can process the old log files.

Second, the implementation of the script

The Nginx log is automatically cut by date script as follows:

#nginx日志切割脚本

#!/bin/bash
#设置日志文件存放目录
logs_path= "/usr/local/nginx/logs/"
#设置pid文件
pid_path= "/usr/local/nginx/nginx.pid"

#重命名日志文件
mv ${logs_path}access.log ${logs_path}access_$ (date-d "Yesterday" + " %y%m%d "). Log

#向nginx主进程发信号重新打开日志
kill-usr1 ' Cat ${pid_path} '

Test environment:

# cat/etc/redhat-release 
Red Hat Enterprise Linux Server Release 5.3 (Tikanga)

#/opt/nginx/nginx-v
nginx ve rsion:nginx/1.6.2

Code:

#!/bin/bash # ============================================================================== # chmod u+x/opt/nginx/  cut_nginx_log.sh # crontab-e # 0 0 * * */opt/nginx/cut_nginx_log.sh >/opt/nginx/logs/cut_nginx_log.log 2>&1 #
============================================================================== LOGS_PATH= "/opt/nginx/logs" archive_year=$ (date-d "Yesterday" "+%y") archive_month=$ (date-d "Yesterday" "+%m") archive_date=$ (date-d "Yesterday" " +%y%m%d_%h%m%s ") if [-r/opt/nginx/nginx.pid]; Then Mkdir-p "${logs_path}/${archive_year}/${archive_month}" mv "${logs_path}/access.log" "${logs_path}/${archive_ Year}/${archive_month}/access_${archive_date}.log "KILL-USR1 $ (cat"/opt/nginx/nginx.pid ") Sleep 1 gzip" ${LOGS_PAT H}/${archive_year}/${archive_month}/access_${archive_date}.log "Else echo" Nginx might is down "fi # ================== ============================================================ # Clean up log files older than # ============================================================================== # change Housekeeping=1 to enable clean up Housekeeping=0 keep_days=100 If [$HOUSEKEEPING = 1]; Then if [-D "${logs_path}"];
  Then find "${logs_path}"-type f-name "access_*.log.gz"-mtime +${keep_days}-exec rm-f {} \; Fi fi

Reference:
Http://wiki.nginx.org/LogRotation

Save the above script nginx_log.sh and set the timed cutting task

Third, regular work
Set up jobs in crontab

Copy Code code as follows:
0 0 * * * bash/usr/local/nginx/nginx_log.sh

This will rename the Nginx log to date format daily 0:0 and regenerate today's new log file.

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.