(编辑:jimmy 日期: 2024/12/23 浏览:2)
先给大家普及下批处理的基本概念
批处理(Batch),也称为批处理脚本。顾名思义,批处理就是对某对象进行批量的处理,通常被认为是一种简化的脚本语言,它应用于DOS和Windows系统中。批处理文件的扩展名为bat 或cmd。目前比较常见的批处理包含两类:DOS批处理和PS批处理。PS批处理是基于强大的图片编辑软件Photoshop的,用来批量处理图片的脚本;而DOS批处理则是基于DOS命令的,用来自动地批量地执行DOS命令以实现特定操作的脚本。更复杂的情况,需要使用if、for、goto等命令控制程式的运行过程,如同C、Basic等高级语言一样。如果需要实现更复杂的应用,利用外部程式是必要的,这包括系统本身提供的外部命令和第三方提供的工具或者软件。批处理程序虽然是在命令行环境中运行,但不仅仅能使用命令行软件,任何当前系统下可运行的程序都可以放在批处理文件中运行。
前言
某qlik sense服务器需要一个health check(健康状态监测)的程序,所以就有了一下的批处理脚本。
大致功能如下:
@echo off color 2E set YYYYmmdd=%date:~0,4%%date:~5,2%%date:~8,2% set hhmiss=%time:~0,2%%time:~3,2%%time:~6,2% set filename=healthcheck.log REM set "filename=healthcheck_%YYYYmmdd%_%hhmiss%.log" set "datetime=%YYYYmmdd%%hhmiss%" set allCount=0 set errorCount=0 set checkList="QlikSenseServiceDispatcher" "QlikSenseSchedulerService" "QlikSenseRepositoryService" "QlikSenseRepositoryDatabase" "QlikSenseProxyService" "QlikSensePrintingService" "QlikSenseEngineService" "QlikLoggingService" echo ============================================ >%filename% echo Health Checking by Qlik Team %filename% echo zhengkai.blog.csdn.net echo (datetime: %datetime% ) %filename% echo ============================================ %filename% echo. %filename% echo. %filename% for %%L in (%checkList%) do call :health_check %%L echo ============================================ %filename% echo Checking Finish. %filename% echo. %filename% echo Error : %errorCount% of %allCount% %filename% echo. %filename% echo ============================================ %filename% call :check_status echo ============================================ %filename% goto:end :health_check sc query |find /i %1 >nul 2>nul if not errorlevel 1 ( echo SUCCESS...... %1 %filename% )else ( echo FAILD...... %1 is error ! %filename% set /a errorCount+=1 ) set /a allCount+=1 goto:eof :check_status if %errorCount% gtr 0 ( echo ###### this server is Not Health %filename% ) else ( echo ###### this server is Health %filename% ) goto:eof :end copy %filename% \\xxxx\DataShare\CSDN\ZhengKai\
批处理获取当前日期时间,生成动态的文件名
set YYYYmmdd=%date:~0,4%%date:~5,2%%date:~8,2% set hhmiss=%time:~0,2%%time:~3,2%%time:~6,2% set "filename=healthcheck_%YYYYmmdd%_%hhmiss%.log" echo %filename%
批处理输出echo到日志文件
echo ============================================ >%filename% echo Health Checking by Qlik Team %filename% echo ============================================ %filename%
批处理遍历列表list
定义在一个List会后,就可以开始使用
set checkList="QlikSenseServiceDispatcher" "QlikSenseSchedulerService" "QlikSenseRepositoryService" "QlikSenseRepositoryDatabase" "QlikSenseProxyService" "QlikSensePrintingService" "QlikSenseEngineService" "QlikLoggingService" echo ============================================ >%filename% echo Health Checking List %filename% for %%L in (%checkList%) do echo %%L echo ============================================ %filename%
批处理函数与call函数
定义一个:function_name,中间是函数内容,后面以goto:eof结尾
call :check_status :check_status if %errorCount% gtr 0 ( echo ###### this server is Not Health %filename% ) else ( echo ###### this server is Health %filename% ) goto:eof
批处理count计数
计数加一 set /a allCount+=1
set allCount=0 for %%L in (%checkList%) do set /a allCount+=1
批处理copy复制文件
copy %filename% "\\xxxx\DataShare\CSDN\ZhengKai\"
批处理大于、小于、等于、不小于、不大于和不等于
if %errorCount% gtr 0 ( echo ###### this server is Not Health %filename% ) else ( echo ###### this server is Health %filename% )
总结
以上所述是小编给大家介绍的BAT批处理判断服务是否正常运行的方法(批处理命令综合应用),希望对大家有所帮助,也非常感谢大家对网站的支持!