批处理监听cpu高于某个值时重启Tomcat服务

@echo off
set secs=300
set srvname="Tomcat7"
set cpuMax=70


echo.
echo ========================================
echo ==         查询计算机服务的状态,     ==
echo ==     每间隔%secs%秒种进行一次查询,     ==
rem echo ==   如发现其cpu超过%cpuMax%,则立即停止并重启。 ==
echo ========================================
echo.
echo 此脚本监测的服务是:%srvname%
echo.


if %srvname%. == . goto end

:chkit
set UseCPU=0
for /f "tokens=2 delims==" %%a in ('wmic path Win32_PerfFormattedData_PerfOS_Processor get PercentProcessorTime /value^|findstr "PercentProcessorTime"') do (
set UseCPU=%%a
)
echo CPU使用率:%UseCPU%
if %UseCPU% GTR %cpuMax% (net stop %srvname% 
					net start %srvname%)
rem 下面的命令用于延时,否则可能会导致cpu单个核心满载。
ping -n %secs% 127.0.0.1 > nul
goto chkit

:end

你可能感兴趣的:(重启Tomcat,java)