How to test whether a service is running from the command line

后端 未结 14 1668
天涯浪人
天涯浪人 2020-12-12 18:59

I would like to be able to query whether or not a service is running from a windows batch file. I know I can use:

sc query \"ServiceName\"

14条回答
  •  隐瞒了意图╮
    2020-12-12 19:41

    SERVICO.BAT
    @echo off
    echo Servico: %1
    if "%1"=="" goto erro
    sc query %1 | findstr RUNNING
    if %ERRORLEVEL% == 2 goto trouble
    if %ERRORLEVEL% == 1 goto stopped
    if %ERRORLEVEL% == 0 goto started
    echo unknown status
    goto end
    :trouble
    echo trouble
    goto end
    :started
    echo started
    goto end
    :stopped
    echo stopped
    goto end
    :erro
    echo sintaxe: servico NOMESERVICO
    goto end
    
    :end
    

提交回复
热议问题