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

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

    @Echo off
    
    Set ServiceName=wampapache64
    
    SC queryex "%ServiceName%"|Find "STATE"|Find /v "RUNNING">Nul&&(
    
    echo %ServiceName% not running
    echo  
    
    Net start "%ServiceName%"
    
    
        SC queryex "%ServiceName%"|Find "STATE"|Find /v "RUNNING">Nul&&(
            Echo "%ServiceName%" wont start
        )
            echo "%ServiceName%" started
    
    )||(
        echo "%ServiceName%" was working and stopping
        echo  
    
        Net stop "%ServiceName%"
    
    )
    
    
    pause
    

提交回复
热议问题