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

后端 未结 14 1626
暖寄归人
暖寄归人 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:38

    You can use the following command to see if a service is running or not:

    sc query [ServiceName] | findstr /i "STATE"
    

    When I run it for my NOD32 Antivirus, I get:

    STATE                       : 4 RUNNING
    

    If it was stopped, I would get:

    STATE                       : 1 STOPPED
    

    You can use this in a variable to then determine whether you use NET START or not.

    The service name should be the service name, not the display name.

提交回复
热议问题