Start a Windows service from a batch script and take appropriate action based on result

jewbix.cube

I have a .bat script that attempts to start a Windows service at the end.

:start_wildfly
echo.
set /p wildfly_service_name="Enter Wildfly service name: "
echo INFO: Starting %wildfly_service_name%...
echo.
call net start "%wildfly_service_name%"

I want to be able to interpret the result of the net start attempt so that I can have my script take the appropriate action if it fails (e.g. if the service is already running, restart it. If the service name is invalid, re-prompt for the name again, if the user doesn't have sufficient privileges, exit).

The problem is that the NET command does not return the documented Win32_Service class codes.

It does echo errors on the console, however:

The requested service has already been started.

More help is available by typing NET HELPMSG 2182.

See http://ss64.com/nt/net_service.html for a list of the errors.

Unforunately, the errorlevel variable is always 2 in these error cases, so I can't rely on that.

What I'm now trying to do is run a FIND on the output of the NET command, searching for specific error codes and act upon them.

net start Wildfly 2>&1 | FIND "2182"
if %errorlevel% equ 0 goto service_already_running

So, the result of the FIND is stored in errorlevel and I can check to see if the FIND succeeded by checking if errorlevel is 0. This works.

Now, the problem comes when I want to check for more than one error code. I don't know how to expand the code above to check for "2185" as well, for example, and goto a different label in that case.

I'm now attempting to store the entire result of the NET command into a variable, and then run a FINDSTR on that variable.

setlocal EnableDelayedExpansion
set "output_cnt=0"
for /F "delims=" %%f in ('dir /b') do (
    set /a output_cnt+=1
    set "output[!output_cnt!]=%%f"
)
for /L %%n in (1 1 !output_cnt!) DO echo !output[%%n]!

This should store and echo each line of the output, however the last line doesn't seem to do anything.

And then I've also found some code that should search within a variable and return whether or not that string was found:

echo.%output%|findstr /C:"2182" >nul 2>&1 && echo Found || echo Not found.

I've had no luck putting it all together though. I just want to be able to interpret the result of the NET START <SERVICE> and jump to certain labels based on the result.

DavidPostill

I want to be able to interpret the result of the net start attempt

so that I can have my script take the appropriate action if it fails (e.g. if the service is already running, restart it. If the service name is invalid, re-prompt for the name again, if the user doesn't have sufficient privileges, exit).

Start the service as you are already doing:

net start "%wildfly_service_name%"

Now check the status of the service.

There are two ways to do this.

  1. Use net start again to see if the service is running:

    net start | find "%wildfly_service_name%" > nul
    if errorlevel 1 echo The service is not running
    
  2. Use sc (Service Control) to check the service status:

    SC query %wildfly_service_name% | find "STATE" | find "STOPPED"
    

    Or

    sc query %wildfly_service_name% | find "STATE" | find "RUNNING"
    

    The two statements above will return %errorlevel% = 1 if the text is not found.


Further Reading

  • An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
  • net - The NET Command is used to manage network resources.
  • sc - Service Control - Create, Start, Stop, Query or Delete any Windows SERVICE.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Windows batch script: selecting an item from a search result list

From Dev

Batch script to start the service in gap of 10 min

From Dev

Batch script to start the service in gap of 10 min

From Dev

Start a program based on a wildcard in a Batch Script

From Dev

Using a service to start an ongoing notification that can take an action

From Dev

Windows Batch start python script with pipe

From Dev

Why can't I start a batch file from a service using system() in Windows Server 2008?

From Dev

Start Excel file from Windows batch script in safemode, use default file association

From Dev

How to start windows service through VB Script?

From Dev

Batch script - how to take last column from a text line

From Dev

Batch script to take file list input from user to feed to WinSCP

From Dev

Batch script to take file list input from user to feed to WinSCP

From Dev

Windows: How to add batch-script action to Right Click menu

From Dev

How to store the result of the expression into a variable in Windows batch script?

From Dev

Folder chooser dialog from a Windows batch script

From Dev

Start a service from a webapp with administrator privileges passing by a batch file

From Dev

If an IOException in Java is thrown by isReachable, what would be an appropriate action to take and why?

From Dev

windows service - start service using batch file using task scheduler on windows 2012 server R2

From Dev

passing parameters from a windows batch script into a powershell script

From Dev

Is systemd service unit option Type=oneshot appropriate for scripts that take a while?

From Dev

How to start an exe from a .NET Windows Service for updating the service

From Dev

Windows Batch - read result from cmd command and save to a variable

From Dev

The windows script adding the appropriate characters to .txt

From Dev

Batch script - IF not returning properly result

From Dev

Unable to start locally-built ntpd from system service script

From Dev

Start a systemd service inside chroot from a non systemd based rootfs

From Dev

Is it possible to run Petrel in batch mode from a windows service?

From Dev

Translating batch to script (Windows)

From Dev

How to take name from file with specific extension and use it as variable in batch script?

Related Related

  1. 1

    Windows batch script: selecting an item from a search result list

  2. 2

    Batch script to start the service in gap of 10 min

  3. 3

    Batch script to start the service in gap of 10 min

  4. 4

    Start a program based on a wildcard in a Batch Script

  5. 5

    Using a service to start an ongoing notification that can take an action

  6. 6

    Windows Batch start python script with pipe

  7. 7

    Why can't I start a batch file from a service using system() in Windows Server 2008?

  8. 8

    Start Excel file from Windows batch script in safemode, use default file association

  9. 9

    How to start windows service through VB Script?

  10. 10

    Batch script - how to take last column from a text line

  11. 11

    Batch script to take file list input from user to feed to WinSCP

  12. 12

    Batch script to take file list input from user to feed to WinSCP

  13. 13

    Windows: How to add batch-script action to Right Click menu

  14. 14

    How to store the result of the expression into a variable in Windows batch script?

  15. 15

    Folder chooser dialog from a Windows batch script

  16. 16

    Start a service from a webapp with administrator privileges passing by a batch file

  17. 17

    If an IOException in Java is thrown by isReachable, what would be an appropriate action to take and why?

  18. 18

    windows service - start service using batch file using task scheduler on windows 2012 server R2

  19. 19

    passing parameters from a windows batch script into a powershell script

  20. 20

    Is systemd service unit option Type=oneshot appropriate for scripts that take a while?

  21. 21

    How to start an exe from a .NET Windows Service for updating the service

  22. 22

    Windows Batch - read result from cmd command and save to a variable

  23. 23

    The windows script adding the appropriate characters to .txt

  24. 24

    Batch script - IF not returning properly result

  25. 25

    Unable to start locally-built ntpd from system service script

  26. 26

    Start a systemd service inside chroot from a non systemd based rootfs

  27. 27

    Is it possible to run Petrel in batch mode from a windows service?

  28. 28

    Translating batch to script (Windows)

  29. 29

    How to take name from file with specific extension and use it as variable in batch script?

HotTag

Archive