BATCH SCRIPT-重新启动服务时不断增长的数据日志

时间:2019-03-28 01:23:28

标签: batch-file

我有一个脚本来停止和启动IIS服务,在此脚本中,它将定义日志和目录。创建的日志正在增长,因为它具有相同的名称目录(下面的代码):

@ECHO About to stop the StackProcess
cd %windir%\system32\inetsrv 
appcmd stop apppool /apppool.name:StackProcess
echo %date% %time% - Service Stopped Successfully > C:\Logs\StackProcess_IISrestart.log
@ECHO StackProcess Stopped

TIMEOUT /t 05 /nobreak

@ECHO StackProcess
cd %windir%\system32\inetsrv 
appcmd start apppool /apppool.name:StackProcess
echo %date% %time% - Service Restarted Successfully > C:\Logs\StackProcess_IISrestart.log
@ECHO StackProcess Restarted

我的下一步是创建一个日志文件,该文件将根据日期戳命名,例如“ StackProcess_IISrestart-YYYYMMDD”,并且该日志文件也会不断增长。

echo off
set CUR_YYYY=%date:~10,4%
set CUR_MM=%date:~4,2%
set CUR_DD=%date:~7,2%

set SUBFILENAME=StackProcess%CUR_YYYY%%CUR_MM%%CUR_DD%

@ECHO About to stop the StackProcess
cd %windir%\system32\inetsrv 
appcmd stop apppool /apppool.name:StackProcess
echo %date% %time% - Service Stopped Successfully > C:\Logs\%SUBFILENAME%.log
@ECHO StackProcess Stopped

TIMEOUT /t 05 /nobreak

@ECHO StackProcess
cd %windir%\system32\inetsrv 
appcmd start apppool /apppool.name:StackProcess
echo %date% %time% - Service Restarted Successfully > C:\Logs\%SUBFILENAME%.log
@ECHO StackProcess Restarted

但是此代码仅在文件将覆盖现有文件的情况下才起作用,这意味着日志文件在当天不会增长。我想在不增加代码的情况下错过它吗?帮帮我,谢谢。

1 个答案:

答案 0 :(得分:1)

如果我理解正确,那么您似乎不了解重定向运算符的工作方式。 > 从新的 >> 附件开始。只需更改为适当的运算符即可解决您的问题。

@Echo Off
Set "log="
For /F "Tokens=1-3Delims=/ " %%A In ('RoboCopy/NJH /L "\|" Null'
)Do If Not Defined log Set "log=StackProcess%%A%%B%%C.log"

Echo Stopping the StackProcess
"%__AppDir__%InetSrv\AppCmd.exe" Stop AppPool /apppool.name:StackProcess
Echo %DATE% %TIME% - Service Stopped Successfully>"%log%"
Echo StackProcess Stopped
Timeout 5 /NoBreak>Nul

Echo Starting the StackProcess
"%__AppDir__%InetSrv\AppCmd.exe" Start AppPool /apppool.name:StackProcess
Echo %DATE% %TIME% - Service Restarted Successfully>>"%log%"
Echo StackProcess Restarted
Timeout 2 /NoBreak>Nul

请注意,尽管我在上面的示例中进行了一些改进,但是您在记录“成功”消息时并未实际验证启动和停止过程是否成功。我将由您决定是否希望扩展代码来满足这种验证要求。