1、Windows下,新建一个Bat文件
2、将以下内容,拷贝到Bta文件:
@echo off
if “%1” == “h” goto begin
mshta vbscript:createobject(“wscript.shell”).run(“%~nx0 h”,0)(window.close)&&exit
:begin
:Nginx
ping 127.0.0.1 -n 300>nul
cd C:\nginx-1.10.1
rem 指定log文件的存放路径
set SrcDir=C:\nginx-1.10.1\logs
set dd=%date:~8,2%
set mm=%date:~5,2%
set yy=%date:~0,4%
set Tss=%TIME:~6,2%
set Tmm=%TIME:~3,2%
set Thh=%TIME:~0,2%
set Thh=%Thh: =0%
rem 移动到其他目录,并根据时间重命名访问日志文件
set “cmdstr1=move %SrcDir%\access.log %SrcDir%\History_Log\Access\10.Nginx%yy%-%mm%-%dd%_%Thh%.%Tmm%.%Tss%.log
call %cmdstr1%”
nginx -s reload
rem 备份到跳板机
set src_dir=C:\nginx-1.10.1\logs\History_Log\Access
set src_back=\10.0.0.7\c$\NginxService\NginxLogAnalyzerService\Log
cd /d %src_dir%
for /f “tokens=*” %%f in (‘dir /b /od /a-d’) do (set f=%%f
set t=%%~tf)
echo 最新的文件是 %f% 修改时间为:%t%
set “accessbak=copy /y %src_dir%\%f% %src_back%\
call %accessbak%”
goto Nginx
解释1:下面这个是自动隐藏,将Bat文件变成进程去执行
if “%1” == “h” goto begin
mshta vbscript:createobject(“wscript.shell”).run(“%~nx0 h”,0)(window.close)&&exit
解释2:下面这个是先定义变量,让其300秒无限循环执行
:Nginx
ping 127.0.0.1 -n 300>nul
……….
goto Nginx
以上,进行设置后,直接双击运行Bat,就可以让Bat文件变成进程在后台自动运行,实现Nginx日志每5分钟自动切割日志了