Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

5. Cut and delete nginx logs

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

5. Nginx log cutting and periodic deletion

LogDir=/data/logs/nginx # log storage directory

LogDate= `date +% Y% m% d` # Log name suffix

Number of days retained by KeepLogDays=30 # (depending on your needs)

DelDate= `date-d "- ${KeepLogDays} days" +% Y% m% d` # the first 30 days timestamp of the current time (is a specific date)

(the-d parameter is to delete the syntax: if date-d 3days is quoted in double quotes, there can be spaces in the middle, otherwise an error will be reported.)

(the +% Y%m%d parameter is the format syntax for printing dates: date +% Y%m%d)

Logs= `ls-l / data/logs/nginx/*.log | the name of the log ending with .log in the directory where the log is stored in awk-Fairbank'{print $log}'`#

[- d $LogDir/$LogDate] | | mkdir $LogDir/$LogDate # determine whether the current timestamp in the log storage directory is the directory. If not, create a new directory.

For i in $Logs

Do

If [- f $LogDir/$i]; then

Touch $LogDir/$LogDate/$i

Ln-sf $LogDir/$LogDate/$i $LogDir/$i

Fi

Done

# create all Logs in the current timestamp directory and create soft chain files in the upper directory

Kill-USR1 `cat / DIR/ nginx.pid` # your nginx.pid storage path is shown in the configuration file

If [x$KeepLogDays! = x0]; then

Rm-rf $LogDir/$DelDate

Fi

# if the variable KeepLogDays is not zero, delete the DelDate variable represents

PS: write this script as a scheduled task and execute it at 00:00 every day

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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report