In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "nginx log cutting shell script sharing", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "nginx log cutting shell script sharing" it!
First, the script idea
The first step is to rename the log file. You don't have to worry about losing the log because nginx can't find the log file after renaming. Before you reopen the log file with the original name, nginx still logs to your renamed file. Linux locates the file by the file descriptor rather than the file name.
The second step is to send the USR1 signal to the nginx main process.
When the nginx master process receives the signal, it reads the log file name from the configuration file, reopens the log file (named after the log name in the configuration file), and uses the user of the worker process as the owner of the log file.
When the log file is reopened, the nginx main process closes the log file with the same name and notifies the worker process to use the newly opened log file.
The worker process immediately opens a new log file and closes the renamed log file.
Then you can deal with the old log files.
II. Script implementation
The script for automatic cutting of nginx logs by date is as follows:
# nginx log slicing script #! / bin/bash# set log file storage directory logs_path= "/ usr/local/nginx/logs/" # set pid file pid_path= "/ usr/local/nginx/nginx.pid" # rename log file mv ${logs_path} access.log ${logs_path} access_$ (date-d "yesterday" + "% Y%m%d") .log # send a message to the nginx main process to reopen the log kill-USR1 `log ${pid_path} `
Test environment:
# cat / etc/redhat-release Red Hat Enterprise Linux Server release 5.3 (Tikanga) # / opt/nginx/nginx-vnginx version: nginx/1.6.2
Code:
#! / bin/bash# = # chmod uplix / opt/nginx/cut_nginx_log.sh# crontab-e # 00 * / opt/nginx/cut_nginx_log.sh > / opt/nginx/logs/cut_nginx_log.log 2 > & opt/nginx/logs = 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_PATH} / ${ARCHIVE_YEAR} / ${ARCHIVE_MONTH} / access_ ${ARCHIVE_DATE} .log "else echo" Nginx might be down "fi# = # Clean up log files older than 100 days# = # Change HOUSEKEEPING=1 to enable clean upHOUSEKEEPING=0KEEP_DAYS=100if [$HOUSEKEEPING= = 1] Then if [- d "${LOGS_PATH}"]; then find "${LOGS_PATH}"-type f-name "access_*.log.gz"-mtime + ${KEEP_DAYS}-exec rm-f {}\; fifi
Reference:
Http://wiki.nginx.org/LogRotation
Save the above script nginx_log.sh and set the scheduled cutting task
III. Regular work
Set up jobs in crontab
The code is as follows:
0 0 * bash / usr/local/nginx/nginx_log.sh
This renames the nginx log to date format at 00:00 every day, and regenerates the new log file for today.
At this point, I believe you have a deeper understanding of "nginx shell script sharing for log cutting". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.