Created
June 5, 2022 06:33
-
-
Save emisjerry/120d6b6a1ddb932650b0b32d54c27a8c to your computer and use it in GitHub Desktop.
Checking the state of a Windows service.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; chk-is-running 服務名稱 系統目錄 啟動批次檔名 | |
;; chk-is-running gitea z:\test\gitea start_service.bat | |
#SingleInstance Force | |
ServiceName = %1% ;; "gitea" | |
ServiceDir = %2% ;;"z:\test\gitea" ;; 系統目錄 | |
StartBat = %3% ;;"start_service.bat" ;; 啟動服務的批次檔 | |
ServiceChk = sc query "%ServiceName%" > %ServiceDir%\chk-is-running.txt | |
runwait, %COMSPEC% /C %ServiceChk%, ,Hide | |
FileRead, FileContent, %ServiceDir%\chk-is-running.txt | |
Loop, Parse, FileContent, `n,`r | |
{ | |
FileLine = %A_LoopField% | |
Lookfor = STATE | |
IfInString, FileLine, %LookFor% | |
{ | |
StringGetPos, pointer, FileLine, : | |
StringRight, ServerStatus, FileLine, StrLen(FileLine) - pointer - 5 | |
if (ServerStatus == "RUNNING") { | |
FileAppend, % PID . " " . A_YYYY . "-" . A_MM . "-" . A_DD . " " . A_Hour . ":" . A_Min . ":" . A_Sec . " " . ServiceName . " is running " . " `n", %ServiceDir%\chk-is-running.log | |
} else { | |
Run, %ServiceDir%\%StartBat% , %ServiceDir%, Hide|UseErrorLevel, PID | |
;;MsgBox %ErrorLevel% | |
FileAppend, % PID . " " . A_YYYY . "-" . A_MM . "-" . A_DD . " " . A_Hour . ":" . A_Min . ":" . A_Sec . " After net start " . ErrorLevel . " `n", %ServiceDir%\chk-is-running.log | |
} | |
} | |
} | |
Return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment