The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 2,387 other followers

Windows Service: Stopping/Starting from the console with a delay

Posted by jpluimers on 2014/02/25

When stopping Windows Services from the command line using `net stop`, then you often get time outs like these:

C:\Windows>net stop "FooBar Survur"
The FooBar Survur service is stopping.
The FooBar Survur service could not be stopped.

C:\Windows>net stop "FooBar Survur"
..
The FooBar Survur service could not be stopped.

Neither the `net stop`, nor the `net start` command allows for specifying a time-out, so it uses the default time-out of 5 seconds. Which often is not long enough.

Luckily, both the Windows sc command and the free 3rd party Windows Service Manager (SrvMan) are able to stop these services.

Note that both tools:

  1. need the internal server name to stop it, so the command-line
  2. require an Administrative UAC token

sc stop internalFooBarSurvur
srvman.exe stop internalFooBarSurvur

You can get the internal service name through my list-services.bat (which does not need an Administrative UAC token):

@echo off
:: find all services by SERVICE_NAME, then list STATE, TYPE, DISPLAY_NAME, and "" (this is on the line below STATE)
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
:: the space in " all" is required by the sc command; see "sc /?" for explanation
:: the ^ in "state^= all" is required as the command interpreter will eat the equals sign, which causes the errors
:: "[SC] EnumQueryServicesStatus OpenService FAILED 1060"
:: "The specified service does not exist as an installed service."
:: see http://blog.smartbear.com/software-quality/managing-windows-services/
:: for /f "usebackq tokens=1,2,3 delims=:" %%i in (`sc query`) do (
for /f "usebackq tokens=1,2,3 delims=:" %%i in (`sc query state^= all`) do (
rem echo %%i %%j %%k
if "%%i"=="SERVICE_NAME" call :%%i %%j %%k
)
endlocal
goto :eof
:SERVICE_NAME
:: echo %0 %1 %2
if "%2"=="" call :process-service %1
if not "%2"=="" call :process-service %1$%2
goto :eof
:process-service
:: echo service "%1"
set service_display_name=
set service_name=
set service_pid=
set service_properties=
set service_state=
set service_type=
:: `sc query` and `sc queryex` will only show DISPLAY_NAME when no SERVICE_NAME is specified
:: so we have to perform `sc query` for ALL services, then grab the DISPLAY_NAME for the matching SERVICE_NAME
for /f "usebackq tokens=1,* delims=:, " %%s in (`sc query state^= all`) do (
rem if "%%s"=="STATE" if not !%1!==!! echo %%v state of %1 is %%v
if "%%s"=="SERVICE_NAME" set service_name=%%t
if "%%s"=="DISPLAY_NAME" if "!service_name!"=="%1" set service_display_name=%%t
set first_char=%%s
set first_char=!first_char:~0,1!
if "!first_char!"=="(" if "!service_name!"=="%1" set service_properties=%%s, %%t
rem echo "!first_char!", !service_properties!, %%s, %%t
)
set service_name=
:: find all services by SERVICE_NAME, then list STATE, TYPE, DISPLAY_NAME, and "" (this is on the line below STATE)
for /f "usebackq tokens=1,2,3,4 delims=:, " %%s in (`sc queryex %1`) do (
rem if "%%s"=="STATE" if not !%1!==!! echo %%v state of %1 is %%v
if "%%s"=="PID" set service_pid=%%t
if "%%s"=="SERVICE_NAME" set service_name=%%t
if "%%s"=="STATE" set service_state=%%u
if "%%s"=="STATE" set service_state=%%u
if "%%s"=="TYPE" set service_type=%%u
if "%%s"=="STATE" set service_state=%%u
rem echo "%%s", "%%t", "%%u", "%%v"
)
echo %service_pid%, %service_state%, %service_type%, %service_name%, %service_properties%, %service_display_name%
goto :eof

view raw
list-services.bat
hosted with ❤ by GitHub

–jeroen

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

 
%d bloggers like this: