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

后端 未结 14 1662
天涯浪人
天涯浪人 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:35

    @ECHO OFF
    REM testing at cmd : sc query "MSSQLSERVER" | findstr RUNNING
    REM "MSSQLSERVER" is the name of Service for sample
    sc query "MSSQLSERVER" %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 Oh noooo.. trouble mas bro
    goto end
    :started
    echo "SQL Server (MSSQLSERVER)" is started
    goto end
    :stopped
    echo "SQL Server (MSSQLSERVER)" is stopped
    echo Starting service
    net start "MSSQLSERVER"
    goto end
    :erro
    echo Error please check your command.. mas bro 
    goto end
    
    :end
    

提交回复
热议问题