|
首先接到这个任务,发现nginx的日志限制更多的都是在Linux下做的,找了半天,也没找到能直接通过nginx.conf更改体现到日志限制上的。
最后决定直接通过bat脚本,来对nginx的日志进行分割和删除。
至于需要谁来执行bat脚本,大家可以根据自己的业务需求来操作,比如:
1.通过系统的任务计划程序
2.通过java程序系统定时器
先来说第一种:
通过创建计划任务,然后选中要执行的bat脚本,设置执行周期,就可以搞定。
第二种:通过服务器内的java程序,定时器调用
1 package com.gosun.check.config.task;2 3 import lombok.extern.slf4j.Slf4j;4 import org.springframework.scheduling.annotation.Scheduled;5 import org.springframework.stereotype.Component;6 7 import java.io.BufferedReader;8 import java.io.File;9 import java.io.InputStream;10 import java.io.InputStreamReader;11 12 @Slf4j13 @Component14 public class PartitionDelLogTask {15 16 @Scheduled(cron = "0 1 0 * * ?") //每天23点执行17 public void delNginxLogTask{18 log.info("===开始执行定时任务===");19 String relativelyPath=System.getProperty("user.dir");20 String batPath = relativelyPath+"\\fenge.bat";21 try {22 File batFile = new File(batPath);23 boolean batFileExist = batFile.exists;24 log.info(">>>> 是否找到bat:{};文件位置:{}",batFileExist,batPath);25 if (batFileExist) {26 callCmd(batPath);27 }28 log.info(">>>> bat文件执行成功");29 } catch (Exception e) {30 log.error(">>>> bat文件执行失败:{}", e.getMessage);31 }32 }33 34 privatestatic void callCmd(String locationCmd){35 StringBuilder sb = new StringBuilder;36 try {37 Process child = Runtime.getRuntime.exec(locationCmd);38 InputStream in = child.getInputStream;39 BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(in));40 String line;41 while((line=bufferedReader.readLine)!=null)42 {43 sb.append(line + "\n");44 }45 in.close;46 try {47 child.waitFor;48 } catch (InterruptedException e) {49 log.info("------异常---------{}",e.getMessage);50 }51 System.out.println("sb:" + sb.toString);52 log.info("------执行完成---------");53 } catch (Exception e) {54 log.info(e.getMessage);55 }56 }57 }则bat脚本:
1 @shift2 3 ::设置nginx安装文件路径和当天时间变量4 @echo off5 set nginx_dir=D:\nginx6 set dir=%nginx_dir%\logs\cut_log7 set log=%nginx_dir%\logs\access.log8 set errorlog=%nginx_dir%\logs\error.log9 set mqttlog=%nginx_dir%\logs\access_mqtt.log10 set today=�te:~0,4%-�te:~5,2%-�te:~8,2%11 set nginxStart = D:\nginx\nginx.exe12 13 ::判断nginx的cut_log目录14 :check_dir15 @echo off16 if exist %dir% (17 goto main18 ) else (19 md "%dir%"20 goto main21 )22 23 ::创建相关目录和对nginx日志进行切割24 :main25 @echo off26 ::结束nginx进程27 taskkill /F /IM nginx.exe > nul28 move "%log%" "%dir%\access-%today%.log" > nul29 move "%errorlog%" "%dir%\error-%today%.log" > nul30 31 if exist %mqttlog% (32 move "%mqttlog%" "%dir%\access_mqtt-%today%.log" > nul33 ) else (34 @echo "no mqttLog"35 )36 37 ::删除指定天数之前的文件38 forfiles /p "d:\nginx\logs\cut_log" /s /m *.* /d -7 /c "cmd /c del @path"39 ::删除文件大于10M的40 for /r d:\nginx\logs\cut_log %%i in (*.log) do @(if %%~zi gtr 10240000 del "%%i" /f)41 set dirr=/d d:/nginx42 echo "%dirr%"43 echo. start Nginx......44 cd "%dirr%"45 IF EXIST "%dirr%nginx.exe" (46 echo "start '' nginx.exe"47 start nginx.exe48 )49 echo.OKbat脚本思路是:
先停止nginx进程 --> 把access.log和error.log剪切到cut_log文件夹中 --> 根据文件创建时间删除7天前的文件 --> 删除文件大于10M的 --> 启动nginx程序
来源:http://www.yidianzixun.com/article/0kTcVlOS
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
x
|