windows - How to test whether a service is running from the command line - Stack Overflow
windows - How to test whether a service is running from the command line - Stack Overflow
command line
Asked 15 years, 9 months ago Modified 1 year, 2 months ago Viewed 326k times
I would like to be able to query whether or not a service is running from a windows batch file. I
know I can use:
70
sc query "ServiceName"
but, this dumps out some text. What I really want is for it to set the errorlevel environment
variable so that I can take action on that.
UPDATE
Thanks for the answers so far. I'm worried the solutions that parse the text may not work on non
English operating systems. Does anybody know a way around this, or am I going to have to bite
the bullet and write a console program to get this right.
windows batch-file
Share Follow edited Dec 9, 2008 at 16:00 asked Dec 9, 2008 at 15:37
Scott Langham
60k 42 133 209
2 I just tested on a China locale Chinese Language Windows 7 laptop, the "sc query ..." command output are
still English. – wangf Aug 31, 2015 at 7:15
To get service state that will be easy to parse by script no matter what OS language is used I have used
WMIC Service WHERE "Name = 'SericeName'" GET Started /format:list . It produces
State=Running - easy to parse by regexp and always in English. – Michał Maciej Gałuszka Feb 19, 2018
at 8:22
98
Share Follow answered Dec 9, 2008 at 15:46
Igal Serban
10.7k 3 36 40
@ShaiAlon, it was powershell that it did not work. It worked on the usual command prompt (cmd.exe).
– Chris Voon Aug 4, 2016 at 9:00
1 It does not work on powershell because sc is an alias to Set-Content. – Chris Voon Aug 4, 2016 at 9:01
6 sc.exe query "ServiceName" | findstr RUNNING will work within Powershell. – Chris Voon Aug 4,
2016 at 9:07
is there a way to use regex search while looking for a particular service name? Sometimes I maynot
remember exactly what the name is but I would know parts of the name.. – alpha_989 Mar 11, 2018 at
18:53
2 If the task is to start a service in case it is not running, then testing for the STOPPED status is much safer,
for the service may also be in one of the transitory states STARTING and STOPPING , when it is best left
alone. – Anton Shepelev Aug 31, 2018 at 16:11
Share Follow edited Nov 10, 2013 at 4:39 answered Mar 23, 2011 at 14:22
RealHowTo Shahin
35.3k 11 72 87 191 1 2
errorlevel is 1 both when the service exists or not. So I can't use "find". (Win 7 Pro 64 bit) – Mogens Beltoft
Oct 8, 2014 at 6:32
@RealHowTo, I am running "MobaSSH", and if I open the "Services" app from Control panel, I can check
that "MobaSSH" is working. Further, the command you mentioned, net start | find "MobaSSH" also
prints out "MobaSSH", indiciating that this command works. However, if I use the sc query "MobaSSH" |
find "RUNNING" command, it shows an error saying "EnumQueryServicesStatus: Open Service Failed
1060". Any idea why these 2 commands are showing different results? – alpha_989 Mar 11, 2018 at 16:57
When I use default Windows services such as "Fax", both commands give similar results. Also sc query
does work.. but its not working for "MobaSSH". is there something special about a service initiated by an
external program? – alpha_989 Mar 11, 2018 at 16:58
Isn't net start the command to start a service? The question is to check if the service is running, not start it
– trees_are_great Jun 6, 2019 at 10:11
1 @trees_are_great using net start alone will list all "started" services. – Nilpo Jun 12, 2021 at 4:25
if you don't mind to combine the net command with grep you can use the following script.
8 @echo off
net start | grep -x "Service"
if %ERRORLEVEL% == 2 goto trouble
if %ERRORLEVEL% == 1 goto stopped
if %ERRORLEVEL% == 0 goto started
echo unknown status
goto end
:trouble
echo trouble
goto end
:started
echo started
goto end
:stopped
echo stopped
goto end
:end
2 I don't seem to have grep on my system. Did you download it from somewhere, if so, where?
– Scott Langham Dec 9, 2008 at 16:04
3 I've used something like this, but with a 'find' instead of a 'grep' because I don't want to have to rely on
installing anything else. – Scott Langham Dec 9, 2008 at 18:50
@ScottLangham Does find have regex searching capability? – alpha_989 Mar 11, 2018 at 16:51
2 @alpha_989 No. If you're on windows, start command prompt and type "find /?" to see the help for it.
– Scott Langham Mar 12, 2018 at 16:46
Grep is not a default windows tool – Mike Q Jul 11, 2023 at 16:31
7 call wmic /locale:ms_409 service where (name="wsearch") get state /value | findstr
State=Running
if %ErrorLevel% EQU 0 (
echo Running
) else (
echo Not running
)
Share Follow answered Dec 28, 2008 at 19:05
NicJ
4,120 1 26 18
Thinking a little bit outside the box here I'm going to propose that powershell may be an answer
on up-to-date XP/2003 machines and certainly on Vista/2008 and newer (instead of .bat/.cmd).
7 Anyone who has some Perl in their background should feel at-home pretty quickly.
$serviceName = "ServiceName";
$serviceStatus = (get-service "$serviceName").Status;
Another way, if you have significant investment in batch is to run the PS script as a one-liner,
returning an exit code.
@ECHO off
SET PS=powershell -nologo -command
%PS% "& {if((get-service SvcName).Status -eq 'Running'){exit 1}}"
ECHO.%ERRORLEVEL%
Running as a one-liner also gets around the default PS code signing policy at the expense of
messiness. To put the PS commands in a .ps1 file and run like powershell myCode.ps1 you may
find signing your powershell scripts is neccessary to run them in an automated way (depends on
your environment). See http://www.hanselman.com/blog/SigningPowerShellScripts.aspx for
details
@ECHO OFF
REM testing at cmd : sc query "MSSQLSERVER" | findstr RUNNING
REM "MSSQLSERVER" is the name of Service for sample
4 sc query "MSSQLSERVER" %1 | findstr RUNNING
if %ERRORLEVEL% == 2 goto trouble
if %ERRORLEVEL% == 1 goto stopped
if %ERRORLEVEL% == 0 goto started
echo unknown status
goto end
:trouble
echo Oh noooo.. trouble mas bro
goto end
:started
echo "SQL Server (MSSQLSERVER)" is started
goto end
:stopped
echo "SQL Server (MSSQLSERVER)" is stopped
echo Starting service
net start "MSSQLSERVER"
goto end
:erro
echo Error please check your command.. mas bro
goto end
:end
Add some explanation with answer for how this answer help OP in fixing current issue – ρяσѕρєя K Dec 23,
2016 at 3:08
Try
or WMIC Service WHERE "Name = 'ServiceName'" GET ProcessId (ProcessId will be zero if service
3
isn't started)
You can set the error level based on whether the former returns "TRUE" or the latter returns
nonzero
3 for example:
Share Follow edited Dec 1, 2016 at 6:32 answered Dec 1, 2016 at 5:08
General Failure Quack
2,597 4 24 52 39 1
seems to do roughly the right thing. But, I'm worried that's not generalized enough to work on
non-english operating systems.
Share Follow edited Nov 10, 2013 at 4:40 answered Dec 9, 2008 at 15:48
RealHowTo Scott Langham
35.3k 11 72 87 60k 42 133 209
If you really need to do i18n, you should write an app which queries and sets the errorlevel. – hometoast
Dec 9, 2008 at 15:49
how to access the answer of the command sc query "ServiceName" | findstr RUNNING so with
conditional statements i can do some processing – Prabhat Mishra Jun 13, 2018 at 7:02
SERVICO.BAT
@echo off
echo Servico: %1
2 if "%1"=="" goto erro
sc query %1 | findstr RUNNING
if %ERRORLEVEL% == 2 goto trouble
if %ERRORLEVEL% == 1 goto stopped
if %ERRORLEVEL% == 0 goto started
echo unknown status
goto end
:trouble
echo trouble
goto end
:started
echo started
goto end
:stopped
echo stopped
goto end
:erro
echo sintaxe: servico NOMESERVICO
goto end
:end
Share Follow edited Dec 5, 2016 at 19:33 answered Dec 5, 2016 at 18:53
thor Luis Ramos
22.3k 31 101 186 21 1
The command below does not work because sc is an alias to Set-Content within Powershell.
find also does not work on Powershell for some reason unknown to me.
I noticed no one mentioned the use of regular expressions when using find / findstr -based
Answers. That can be problematic for similarly named services.
1
Lets say you have two services, CDPUserSvc and CDPUserSvc_54530
If you use most of the find / findstr -based Answers here so far, you'll get false-positives for
CDPUserSvc queries when only CDPUserSvc_54530 is running.
The /r and /c switches for findstr can help us handle that use-case, as well as the special
character that indicates the end of the line, $
This query will only verify the running of the CDPUserSvc service and ignore CDPUserSvc_54530
sc query|findstr /r /c:"CDPUserSvc$"
A suggested approach can be found in an answer to a related question: target very specific things
which are locale-independent. In this case, for obtaining service status from sc.exe , the integer
1 which identifies the state was found to be independent of active language and, therefore, robust.
Share Follow edited Jun 16, 2023 at 11:05 answered Jun 15, 2023 at 23:03
Helder Magalhães
620 8 15
0 sc query "SomeService" |grep -qo RUNNING && echo "SomeService is running." || echo
"SomeService is not running!"
Your answer is a bit hard to read and understand. Please clarify what it does, and ideally convert the code
into multiple lines so it's easier to grasp. – CherryDT Nov 2, 2021 at 10:29