On the other hand, issuing a "net stop" to a service not running does no harm at all...
Main Topics
Browse All TopicsI am wondering from the command line or using a batch file (.bat) how can I check if a certain service running or not ?!
example: Microsoft Exchange System Attendant
because I want the batch file to Stop the service only if it is running.
Thanks
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
I have set the following batch file the erro level I am getting is fine from the command
SC query | FINDstr /c:"Messenger"
and I can varify it by using the command
echo The Error Leve is: %ERRORLEVEL%
.. BUT, at the IF statemetn the ErrorLevel is always 0 (zero) for some reason !!
.. so it always go to the section :Running in my case
....
-- Any idea ?
... The batch file is as follows
@echo off
echo.
cls
SC query | FINDstr /c:"Messenger"
REM show error level befor JUMPING
echo The Error Leve is: %ERRORLEVEL%
if errorlevel 0 GOTO Running
if errorlevel 1 GOTO NotRunning
REM jump to End if Errorlevel is not 0 nor 1 = A PROBLEM ...
GOTO End
:Running
echo.
echo The Service .. Messenger .. is Running ...
GOTO End
:NotRunning
echo.
echo The Service .. Messenger .. is Not Running ...
:End
When you use
if errorlevel x
it is actually checking for if the errorlevel is x or more, you can either check in reverse order:
rem if error is 1 or more:
if errorlevel 1 goto notrunning
goto running
or use
if %errorlevel%==0 goto running
if %errorlevel%==1 goto notrunning
as then you are checking an environment variable so you can do checks for equaling a specific number etc.
But as people have said pretty well you can just do NET STOP "Yourservice" as it doesn't really matter if is already started or not.
hth
Steve
Business Accounts
Answer for Membership
by: mpfisterPosted on 2007-11-07 at 03:40:23ID: 20231185
sc query | findstr /C:"Microsoft Exchange System Attendant"
if errorlevel 1 net stop "Microsoft Exchange System Attendant"
Hope it helps,
Michael