在Linux上Shell很强大,监控进程很简单,如果挂掉可以很方便的拉起来。
今天公司要监控Windows上的Python进程状态,如果挂掉也需要拉起来,也不知道有没有什么现成的工具,就用bat撸了一个,顺便记录下。

@echo off
set LOG_FILE=log.txt
:loop
set is_running=0
for /f "skip=1" %%i in ('wmic process where "name='python.exe'" get name ^| findstr /r /v "^$"') do set is_running=1
if "%is_running%"=="1" (
    echo Process is running.
) else (
    echo Process is not running. Restarting...
    start cmd /k python d:\path\xxx.py
    echo %date% %time% Process restarted. >> %LOG_FILE%
)
timeout /T 5 >NUL
goto loop

每5秒检测一下进程状态,如果挂了就重启进程,路径那块需要自己修改。

Last modification:June 11, 2023
If you think my article is useful to you, please feel free to appreciate