How to check if a service is running via batch file and start it, if it is not running?

后端 未结 14 1614
暖寄归人
暖寄归人 2020-11-30 18:15

I want to write a batch file that performs the following operations:

  • Check if a service is running
    • If is it running, quit the batch
    • If
14条回答
  •  情书的邮戳
    2020-11-30 18:21

    Language independent version.

    @Echo Off
    Set ServiceName=Jenkins
    
    
    SC queryex "%ServiceName%"|Find "STATE"|Find /v "RUNNING">Nul&&(
        echo %ServiceName% not running 
        echo Start %ServiceName%
    
        Net start "%ServiceName%">nul||(
            Echo "%ServiceName%" wont start 
            exit /b 1
        )
        echo "%ServiceName%" started
        exit /b 0
    )||(
        echo "%ServiceName%" working
        exit /b 0
    )
    

提交回复
热议问题