WinServer 服务自动重启和进程监控重启脚本

sengMsg.exe 为对接短信网关的命令行程序

服务自动重启并清理垃圾缓存

@echo off
setlocal enabledelayedexpansion

:: 设置服务名称和日志文件夹路径
set SERVICE_NAME=postgresql16
set LOGS_DIR=D:\106check\test
set MAX_RETRIES=3
set WAIT_TIME=1

:: 请勿编辑以下部分
set RETRY_COUNT=0

:: 停止服务
echo 正在停止服务 %SERVICE_NAME%...
sc stop %SERVICE_NAME%
timeout /t %WAIT_TIME% /nobreak >nul

:: 删除日志文件
echo 正在删除日志文件...
del /q "%LOGS_DIR%\*"
echo 日志文件已删除。

:: 重新启动服务并检查状态
:retry
set /a RETRY_COUNT+=1
echo 尝试启动服务 %SERVICE_NAME%,尝试次数:%RETRY_COUNT%...
sc start %SERVICE_NAME%
timeout /t %WAIT_TIME% /nobreak >nul

:: 检查服务状态
for /f "tokens=3 delims=: " %%a in ('sc query %SERVICE_NAME% ^| findstr "STATE"') do set SERVICE_STATUS=%%a
if "!SERVICE_STATUS!"=="RUNNING" (
    echo 服务 %SERVICE_NAME% 成功启动。
    goto send_message
) else (
    if !RETRY_COUNT! lss %MAX_RETRIES% (
        echo 服务 %SERVICE_NAME% 启动失败,正在重试...
        goto retry
    ) else (
        echo 服务 %SERVICE_NAME% 启动失败。
        goto send_message
    )
)

:: 成功启动服务后发送短信
:send_message
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set datetime=%%a
set CURRENT_TIME=!datetime:~0,4!-!datetime:~4,2!-!datetime:~6,2! !datetime:~8,2!:!datetime:~10,2!:!datetime:~12,2!

sendMsg.exe "%CURRENT_TIME%" "%SERVICE_NAME%" "%SERVICE_STATUS%"

goto end


:end
endlocal

进程监控重启脚本

@echo off
setlocal enabledelayedexpansion

:: 设置监控频率(单位:秒)
set "FREQUENCY=5"
:: 设置重启进程的尝试次数
set "RETRY_LIMIT=3"
:: 设置重启进程的开关(1为开启,0为关闭)
set "RESTART_ENABLED=1"
:: 设置短信发送开关(1为开启,0为关闭)
set "SMS_ENABLED=1"

:: 定义进程和路径(每个PROCESSn代表一个进程及其路径)
set "PROCESS1=chrome.exe;C:\Program Files\Google\Chrome\Application\chrome.exe"
set "PROCESS2=code.exe;C:\Users\ipc\AppData\Local\Programs\Microsoft VS Code\code.exe"
:: 添加新进程,在下一行继续定义 PROCESS3, PROCESS4, ...
set "PROCESS3=NewApp.exe;C:\Path\To\NewApp.exe"
:: 定义进程数量
set "PROCESS_COUNT=2"

:: 主监控循环
:monitor

for /l %%i in (1,1,%PROCESS_COUNT%) do (
    set "PROCESS_INFO=!PROCESS%%i!"
    for /f "tokens=1,2 delims=;" %%a in ("!PROCESS_INFO!") do (
        set "PROCESS_NAME=%%a"
        set "PROCESS_PATH=%%b"
        for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set datetime=%%a
        set CURRENT_TIME=!datetime:~0,4!-!datetime:~4,2!-!datetime:~6,2! !datetime:~8,2!:!datetime:~10,2!:!datetime:~12,2!
        :: 检查当前进程是否运行
        wmic process where "name='!PROCESS_NAME!'" get ExecutablePath | findstr /i /c:"!PROCESS_PATH!" > nul
        if errorlevel 1 (
            echo !PROCESS_NAME! at !PROCESS_PATH! is not running.
            if "!RESTART_ENABLED!"=="1" (
                set /a RETRY_COUNT=0
                :retry_restart
                if !RETRY_COUNT! lss %RETRY_LIMIT% (
                    set /a RETRY_COUNT+=1
                    echo Attempting to restart !PROCESS_NAME! [Attempt !RETRY_COUNT! of %RETRY_LIMIT%]...
                    start "" "!PROCESS_PATH!"
                    timeout /t %FREQUENCY% > nul
                    wmic process where "name='!PROCESS_NAME!'" get ExecutablePath | findstr /i /c:"!PROCESS_PATH!" > nul
                    if errorlevel 1 (
                        goto retry_restart
                    ) else (
                        echo !PROCESS_NAME! restarted successfully.
                        :: 在此处调用您的短信发送逻辑,例如进程成功重启
                        if "!SMS_ENABLED!"=="1" (
                            sendMsg.exe "!CURRENT_TIME!" "!PROCESS_NAME!" "RUNNING" "!RETRY_COUNT!" "!RETRY_LIMIT!"
                            set "SMS_ENABLED=1" 
                        )
                        set /a RETRY_COUNT=0
                    )
                ) else (
                    echo Failed to restart !PROCESS_NAME! after %RETRY_LIMIT% attempts.
                    :: 在此处调用您的短信发送逻辑,例如进程重启失败
                    if "!SMS_ENABLED!"=="1" (
                        sendMsg.exe "!CURRENT_TIME!" "!PROCESS_NAME!" "STOPPED" "!RETRY_COUNT!" "!RETRY_LIMIT!"
                        set "SMS_ENABLED=0"
                    )
                    set /a RETRY_COUNT=0
                )
            ) else (
                echo Restart is disabled.
                :: 在此处调用您的短信发送逻辑,例如进程未运行
                if "!SMS_ENABLED!"=="1" (
                    sendMsg.exe "!CURRENT_TIME!" "!PROCESS_NAME!" "STOPPED" "0" "0"
                    set "SMS_ENABLED=0" 
                )
                set /a RETRY_COUNT=0
            )
        ) else (
            echo !PROCESS_NAME! at !PROCESS_PATH! is running.
            set "SMS_ENABLED=1"  :: 进程运行正常时也重置短信发送开关
        )
    )
)

:: 等待指定的监控频率时间再次进行监控
timeout /t %FREQUENCY% > nul
goto monitor

:end
endlocal

评论已关闭