BAT批處理判斷服務是否正常執行(批處理命令綜合應用)

Moshow鄭鍇發表於2020-04-07

前言

qlik sense伺服器需要一個health check(健康狀態監測)的程式,所以就有了一下的批處理指令碼。

大致功能如下:

  • 批處理獲取當前日期時間
  • 批處理生成動態的檔名
  • 批處理輸出echo到日誌檔案
  • 批處理遍歷列表list
  • 批處理函式與call函式
  • 判斷服務是否執行中
  • 批處理count計數
  • 批處理copy複製檔案
  • 批處理判斷大於gtr(記住那輛跑車名,批處理大於、小於、等於、不小於、不大於和不等於)
@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會後,就可以開始使用

  • for %%L 用什麼變數來遍歷
  • in (%checkList%) 遍歷哪個陣列
  • do echo %%L 遍歷來做什麼
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 檔名 目錄頁
  • 複製資料夾用xcopy,指定的目錄連檔案和目錄結構一併拷貝
copy %filename% "\\xxxx\DataShare\CSDN\ZhengKai\"

批處理大於、小於、等於、不小於、不大於和不等於

  • EQU - 等於
  • NEQ - 不等於
  • LSS - 小於
  • LEQ - 小於或等於
  • GTR - 大於
  • GEQ - 大於或等於
if %errorCount% gtr 0 (
	echo ###### this server is Not Health          >>%filename%
) else (
	echo ###### this server is Health              >>%filename%
)

相關文章