设为首页 加入收藏

TOP

WINDOWS下对NIGNX日志文件进行限制(一)
2023-07-25 21:34:47 】 浏览:56
Tags:WINDOWS 下对 NIGNX 文件进

  首先接到这个任务,发现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 @Slf4j
13 @Component
14 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     private static 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 @shift
 2 
 3 ::设置nginx安装文件路径和当天时间变量
 4 @echo off
 5 set nginx_dir=D:\nginx
 6 set dir=%nginx_dir%\logs\cut_log
 7 set log=%nginx_dir%\logs\access.log
 8 set errorlog=%nginx_dir%\logs\error.log
 9 set mqttlog=%nginx_dir%\logs\access_mqtt.log
10 set today=%date:~0,4%-%date:~5,2%-%date:~8,2%
11 set nginxStart = D:\nginx\nginx.exe
12 
13 ::判断nginx的cut_log目录
14 :check_dir
15 @echo off
16 if exist %dir% (
17     goto main
18 ) else (
19     md "%dir%"
20     goto main
21 )
22 
23 ::创建相关目录和对nginx日志进行切割
24 :main
25 @echo off
26 ::结束nginx进程
27 taskkill /F /IM nginx.exe > nul
28 move "%log%" "%dir%\access-%today%.log" > nul
29 move "%errorlog%" "%dir%\error-%today%.log" > nul
30 
31 if exist %mqttlog% (
32     move "%mqttlog%" "%dir%\access_mqtt-%today%.log" > nul
33 ) 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:/nginx
42 echo "%dirr%"
43 echo. start Nginx......
44 cd "%dirr%"
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇P2项目答辩总结 下一篇对Java Web中WEB-INF目录的理解以..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目