Checking service status?

  • Thread starter Thread starter Antti H
  • Start date Start date
A

Antti H

Hello,
I am trying to improve our script that controls some services on our
W2003 server.
My goal is to determine in the batch file if the service actually goes
down/up as supposed.


See:

<
E:\test>net stop spooler
The Print Spooler service is stopping.
The Print Spooler service was stopped successfully.


E:\test>tasklist /SVC | find /C "Spooler"
0

E:\test>echo %ERRORLEVEL%
0

errorlevel should be 1 instead of 0 ,no?

If i could set the output of find to a variable, i would have no problem
checking if %variable%==0 or similar. Is there any way to set variables
with the output of a command inside batch, using only windows tools?

Thank you all very much in advance,
Antti H


PS: Can anyone recommend a book that is useful as a command reference
with examples for windows servers?
 
@echo off
setlocal
set OK=N
for /f "Tokens=*" %%a in ('net stop spooler^|find /i "stopped successfully"') do (
set OK=Y
)
If "%OK%" EQU "N" (
@echo The Spooler server did not stop successfully.
) ELSE (
@echo The Spooler server stopped successfully.
)
endlocal

Hello,
I am trying to improve our script that controls some services on our
W2003 server.
My goal is to determine in the batch file if the service actually goes
down/up as supposed.


See:

<
E:\test>net stop spooler
The Print Spooler service is stopping.
The Print Spooler service was stopped successfully.


E:\test>tasklist /SVC | find /C "Spooler"
0

E:\test>echo %ERRORLEVEL%
0


errorlevel should be 1 instead of 0 ,no?

If i could set the output of find to a variable, i would have no problem
checking if %variable%==0 or similar. Is there any way to set variables
with the output of a command inside batch, using only windows tools?

Thank you all very much in advance,
Antti H


PS: Can anyone recommend a book that is useful as a command reference
with examples for windows servers?


Jerold Schulman
Windows Server MVP
JSI, Inc.
http://www.jsiinc.com
 
In addition to my porevious reply, Windows Server 2003 (and Windows XP) had the SC command.


sc query spooler

SERVICE_NAME: spooler
TYPE : 110 WIN32_OWN_PROCESS (interactive)
STATE : 4 RUNNING
(STOPPABLE,NOT_PAUSABLE,ACCEPTS_SHUTDOWN)
WIN32_EXIT_CODE : 0 (0x0)
SERVICE_EXIT_CODE : 0 (0x0)
CHECKPOINT : 0x0
WAIT_HINT : 0x0

sc stop Spooler

SERVICE_NAME: Spooler
TYPE : 110 WIN32_OWN_PROCESS (interactive)
STATE : 3 STOP_PENDING
(NOT_STOPPABLE,NOT_PAUSABLE,IGNORES_SHUTDOWN)
WIN32_EXIT_CODE : 0 (0x0)
SERVICE_EXIT_CODE : 0 (0x0)
CHECKPOINT : 0x0
WAIT_HINT : 0x0

sc query spooler

SERVICE_NAME: spooler
TYPE : 110 WIN32_OWN_PROCESS (interactive)
STATE : 1 STOPPED
(NOT_STOPPABLE,NOT_PAUSABLE,IGNORES_SHUTDOWN)
WIN32_EXIT_CODE : 0 (0x0)
SERVICE_EXIT_CODE : 0 (0x0)
CHECKPOINT : 0x0
WAIT_HINT : 0x0


You can 'sc stop spooler' or 'net stop spooler' and then:
for /f "Tokens=1-2* Delims=: " %%a in ('sc query spooler^|FIND "STATE"') do set STATE=%%b
if "%STATE%" EQU "1" goto stopped


Hello,
I am trying to improve our script that controls some services on our
W2003 server.
My goal is to determine in the batch file if the service actually goes
down/up as supposed.


See:

<
E:\test>net stop spooler
The Print Spooler service is stopping.
The Print Spooler service was stopped successfully.


E:\test>tasklist /SVC | find /C "Spooler"
0

E:\test>echo %ERRORLEVEL%
0


errorlevel should be 1 instead of 0 ,no?

If i could set the output of find to a variable, i would have no problem
checking if %variable%==0 or similar. Is there any way to set variables
with the output of a command inside batch, using only windows tools?

Thank you all very much in advance,
Antti H


PS: Can anyone recommend a book that is useful as a command reference
with examples for windows servers?


Jerold Schulman
Windows Server MVP
JSI, Inc.
http://www.jsiinc.com
 
Jerold Schulman said:
@echo off
setlocal
set OK=N
for /f "Tokens=*" %%a in ('net stop spooler^|find /i "stopped successfully"') do (
set OK=Y
)
If "%OK%" EQU "N" (
@echo The Spooler server did not stop successfully.
) ELSE (
@echo The Spooler server stopped successfully.
)
endlocal

Any reason why this wouldn't work:

net stop spooler | find /i "stopped successfully"
if errorlevel 1 (
echo The Spooler server did not stop successfully.
) ELSE (
echo The Spooler server stopped successfully.
)

/Al
 
I tend not to rely on ERRORLEVEL as I found that it is too often 'broken' between various O/S versions and hotfixes and Service Packs.

Any reason why this wouldn't work:

net stop spooler | find /i "stopped successfully"
if errorlevel 1 (
echo The Spooler server did not stop successfully.
) ELSE (
echo The Spooler server stopped successfully.
)

/Al


Jerold Schulman
Windows Server MVP
JSI, Inc.
http://www.jsiinc.com
 
Back
Top