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

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

    That should do it:

    FOR %%a IN (%Svcs%) DO (SC query %%a | FIND /i "RUNNING"
    IF ERRORLEVEL 1 SC start %%a)
    
    0 讨论(0)
  • 2020-11-30 18:29

    I just found this thread and wanted to add to the discussion if the person doesn't want to use a batch file to restart services. In Windows there is an option if you go to Services, service properties, then recovery. Here you can set parameters for the service. Like to restart the service if the service stops. Also, you can even have a second fail attempt do something different as in restart the computer.

    0 讨论(0)
  • 2020-11-30 18:34

    For Windows server 2012 below is what worked for me. Replace only "SERVICENAME" with actual service name:

    @ECHO OFF
    SET SvcName=SERVICENAME
    
    SC QUERYEX "%SvcName%" | FIND "STATE" | FIND /v "RUNNING" > NUL && (
        ECHO %SvcName% is not running 
        ECHO START %SvcName%
    
        NET START "%SvcName%" > NUL || (
            ECHO "%SvcName%" wont start 
            EXIT /B 1
        )
        ECHO "%SvcName%" is started
        EXIT /B 0
    ) || (
        ECHO "%SvcName%" is running
        EXIT /B 0
    )
    
    0 讨论(0)
  • 2020-11-30 18:36

    I also wanted an email sent if the service was started this way so added a bit to @Ic code just thought I would post it in case it helped anyone. I used SendMail but there are other command line options How to send a simple email from a Windows batch file?

    set service=MyServiceName
    
    for /F "tokens=3 delims=: " %%H in ('sc query %service% ^| findstr "        STATE"') do (
      if /I "%%H" NEQ "RUNNING" (
    
        net start %service%
    
        for /F "tokens=3 delims=: " %%H in ('sc query %service% ^| findstr "        STATE"') do (
          if /I "%%H" EQ "RUNNING" (
            SendMail /smtpserver localhost /to me@mydomain.com /from watchdog@mydomain.com /subject Service Autostart Notification /body Autostart on service %service% succeded.
          ) else (
            SendMail /smtpserver localhost /to me@mydomain.com /from watchdog@mydomain.com /subject Service Autostart Notification /body Autostart on service %service% failed.
          )
        )
    
      )
    )
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-30 18:45
    @echo off
    
    color 1F
    
    
    @sc query >%COMPUTERNAME%_START.TXT
    
    
    find /I "AcPrfMgrSvc" %COMPUTERNAME%_START.TXT >nul
    
    IF ERRORLEVEL 0 EXIT
    
    IF ERRORLEVEL 1 NET START "AcPrfMgrSvc"
    
    0 讨论(0)
提交回复
热议问题