none
How do I check if service is running in batch?

    Question

  • On windows XP, we have a batch file that runs at startup and we are using port reporter to help monitor certain machines.  ALL machines have the service but it is disabled until we manually turn it on.  How do I go about using a batch file to check if it is on


    For Example:

    If service "Port Reporter" = on (
         copy c:\logfiles <server that will hold logfiles>
    )

     and secondly how would I be able to set the service to on and automatic via command line.....  I know how to turn it on just not automatic  (net start spooler is an example)
    Friday, March 05, 2010 3:17 PM

Answers

  • If the service's name is reported as "Port Reporter", then this should work ...

      net start | find /i "Port Reporter" > nul
      if not errorlevel 1 copy c:\logfiles \\server that holds logfiles\sharename\folder

    Check the exact name reported at a command prompt by typing ...

    C:\>net start

    and look for a match in the list that is reported.

    However, considering your second request, a better way might be to use the wmic.exe utility, instead.  It is available on OSs from WinXP up, I believe.  That approach would look something like this ...

      wmic service where "caption = 'Port Reporter'" get started | find "TRUE" > nul
      if errorlevel 1 (
        wmic service where "caption = 'Port Reporter'" set startmode=Auto > nul
        net start "Port Reporter" > nul
      )
      wmic service where "caption = 'Port Reporter'" get started | find "TRUE" > nul
      if errorlevel 1 (echo Service process Failed & goto :EOF)
      copy c:\logfiles \\server that holds logfiles\sharename\folder


    Tom Lavedas
    Friday, March 05, 2010 4:01 PM
    Moderator


  • SC QUERY PORT REPORTER | findstr /i "STATE"

    That'll tell you if it's running or not.

    To turn it on and set Automatic:

    SC CONFIG PORT REPORTER START= AUTO
    SC START PORT REPORTER

    Keep in the mind that PORT REPORTER in the above examples would need to be whatever the service name is, not the display name. To find the service name, just run the SC QUERY command and find it in the list.

    Friday, March 05, 2010 4:17 PM
  • To escape percent signs, double them up ...

    echo If %%firstrun%%==No goto Startcam >> c:\tools\csstart.bat

    Note that the resulting statement fails if FirstRun is NOT defined when the batch file (cssstart.bat) is executed.  A better approach is something like this ...

    echo If [%%firstrun%%]==[No] goto Startcam >> c:\tools\csstart.bat

    This approach skips the goto part, if Firstrun is not defined at run time.  If that is not appropriate then another iff to test the existence ofthe variable should precede this test.



    Tom Lavedas
    Friday, March 05, 2010 6:50 PM
    Moderator

All replies

  • If the service's name is reported as "Port Reporter", then this should work ...

      net start | find /i "Port Reporter" > nul
      if not errorlevel 1 copy c:\logfiles \\server that holds logfiles\sharename\folder

    Check the exact name reported at a command prompt by typing ...

    C:\>net start

    and look for a match in the list that is reported.

    However, considering your second request, a better way might be to use the wmic.exe utility, instead.  It is available on OSs from WinXP up, I believe.  That approach would look something like this ...

      wmic service where "caption = 'Port Reporter'" get started | find "TRUE" > nul
      if errorlevel 1 (
        wmic service where "caption = 'Port Reporter'" set startmode=Auto > nul
        net start "Port Reporter" > nul
      )
      wmic service where "caption = 'Port Reporter'" get started | find "TRUE" > nul
      if errorlevel 1 (echo Service process Failed & goto :EOF)
      copy c:\logfiles \\server that holds logfiles\sharename\folder


    Tom Lavedas
    Friday, March 05, 2010 4:01 PM
    Moderator


  • SC QUERY PORT REPORTER | findstr /i "STATE"

    That'll tell you if it's running or not.

    To turn it on and set Automatic:

    SC CONFIG PORT REPORTER START= AUTO
    SC START PORT REPORTER

    Keep in the mind that PORT REPORTER in the above examples would need to be whatever the service name is, not the display name. To find the service name, just run the SC QUERY command and find it in the list.

    Friday, March 05, 2010 4:17 PM
  • one last question?  If I building a batch file with another batch file (yes I know it is overkill, but when using ghost it is easier to build it at rebuild time) how do I pass a variable name.  I dont want to pass the value.

    example:

    echo If %firstrun%==No goto Startcam >> c:\tools\csstart.bat

    I want to use the actual %firtrun% in batch file and not value.  I know in some programming language you can use the "\" to escape the meaning and use the literal name.  so what is the escape character in batch?
    Friday, March 05, 2010 6:41 PM
  • To escape percent signs, double them up ...

    echo If %%firstrun%%==No goto Startcam >> c:\tools\csstart.bat

    Note that the resulting statement fails if FirstRun is NOT defined when the batch file (cssstart.bat) is executed.  A better approach is something like this ...

    echo If [%%firstrun%%]==[No] goto Startcam >> c:\tools\csstart.bat

    This approach skips the goto part, if Firstrun is not defined at run time.  If that is not appropriate then another iff to test the existence ofthe variable should precede this test.



    Tom Lavedas
    Friday, March 05, 2010 6:50 PM
    Moderator